SwingWorker does not update JProgressBar without Thread.sleep() in custom dialog panel

The setProgress() API notes: “For performance purposes all these invocations are coalesced into one invocation with the last invocation argument only.” Adding Thread.sleep(1) simply defers the coalescence; invoking println() introduces a comparable delay. Take heart that your file system is so fast; I would be reluctant to introduce an artificial delay. As a concrete example … Read more

WatchService and SwingWorker: how to do it correctly?

Because your background thread is devoted entirely to watching, take() is the right choice. It effectively hides the platform dependent implementation, which may either forward or poll. One of the poll() methods would be appropriate if, for example, your background thread also needed to examine other queues in series with the WatchService. Addendum: Because the … Read more

JComponents disappearing after calling mouseClicked()

Consider using a JList for the right panel to take advantage of the flexible layout options and selection handling, as shown here and below. import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import javax.swing.BorderFactory; import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListModel; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import … Read more

Java socket swingWorker running but no message received or transmitted

Here’s a VERY basic example. Basically, because you program requires asynchronous communications (that is, you need to be able to read from the socket AND write to it at the same time), you need to offload each stream to a separate thread. The management process of this example is, well, no existent. Realistically, you should … Read more

GUI running at 30 fps?

It turns out SwingWorker posts instances of Runnable to the EventQueue using a javax.swing.Timer with exactly that DELAY. private static class DoSubmitAccumulativeRunnable extends AccumulativeRunnable<Runnable> implements ActionListener { private final static int DELAY = (int) (1000 / 30); … } You can get a higher frame rate, but not with javax.swing.SwingWorker. You can build SwingWorker from … Read more

java swingworker thread to update main Gui

There is an excellent example from the JavaDocs class PrimeNumbersTask extends SwingWorker<List<Integer>, Integer> { PrimeNumbersTask(JTextArea textArea, int numbersToFind) { //initialize } @Override public List<Integer> doInBackground() { List<Integer> numbers = new ArrayList<Integer>(25); while (!enough && !isCancelled()) { number = nextPrimeNumber(); numbers.add(number); publish(number); setProgress(100 * numbers.size() / numbersToFind); } return numbers; } @Override protected void process(List<Integer> chunks) … Read more

tech