How do I make my SwingWorker example work properly?

Here an updated version of your code which works import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JProgressBar; import javax.swing.SwingWorker; public class SwingTesting { public static void main(String[] args) { EventQueue.invokeLater( new Runnable() { @Override public void run() { JFrame frame = new JFrame(); JButton button = new … Read more

Setting the maximum size of a JDialog?

As discussed in Sizing a Scroll Pane, some components can provide useful information about setting the viewport’s preferred size. The setVisibleRowCount() method of JList is particularly convenient, but even getViewport().setPreferredSize(…) may suffice. Naturally, an sscce would help. Addendum: As a concrete example, the dialog below is initially sized to have N-2 rows. As more are … Read more

Remove Top-Level Container on Runtime

Invoking dispose() allows the host platform to reclaim memory consumed by the heavyweight peer, but it can’t do so until after the WINDOW_CLOSING event is processed on the EventQueue. Even then, gc() is a suggestion. Addendum: Another way to see the nightmare is via a profiler. Running the example below with jvisualvm, one can see … Read more