Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here an updated version of your code which works</p> <pre><code>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 JButton(); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new GuiWorker().execute(); } }); button.setText("Test Me"); frame.getContentPane().add(button); frame.pack(); frame.setVisible(true); } } ); } } class GuiWorker extends SwingWorker&lt;Integer, Integer&gt; { /* * This should just create a frame that will hold a progress bar until the * work is done. Once done, it should remove the progress bar from the dialog * and add a label saying the task complete. */ private JFrame frame = new JFrame(); private JDialog dialog = new JDialog(frame, "Swingworker test", true); private JProgressBar progressBar = new JProgressBar(); public GuiWorker() { progressBar.setString("Waiting on time"); progressBar.setStringPainted(true); progressBar.setIndeterminate(true); dialog.getContentPane().add(progressBar); dialog.pack(); dialog.setModal( false ); dialog.setVisible(true); } @Override protected Integer doInBackground() throws Exception { System.out.println( "GuiWorker.doInBackground" ); Thread.sleep(1000); return 0; } @Override protected void done() { System.out.println("done"); JLabel label = new JLabel("Task Complete"); dialog.getContentPane().remove(progressBar); dialog.getContentPane().add(label); dialog.getContentPane().validate(); } } </code></pre> <p>Key point is that setting a model dialog visible blocks until the dialog is disposed. So making it non-modal fixed it + the <code>validate</code> call on the content pane when you switch components. I also adjusted your main method to run on the EDT, and added some System.out calls. If you remove the <code>setModal( false )</code> call you will see those statements are not printed until you close the dialog</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload