1

I want to create a basic JDialog with a progress bar, and to update the bar when some operations are done. My code is:

public class Main {

public static void main(String[] args) {

    WikiReaderUI ui = new WikiReaderUI();
    SwingUtilities.invokeLater(ui);
}}

and:

public class WikiReaderUI implements Runnable {

private JFrame frame;
protected Document doc;
protected JProgressBar progressBar;
protected int progress;

@Override
public void run() {
    frame = new JFrame("Wiki READER");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    // Display the window.
    frame.setSize(600, 320);
    frame.setResizable(false);
    frame.setVisible(true);

}

private void addComponentsToPane(Container pane) {
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    addLanguagePanel(pane);
    //other panels...irelevant for my problem
    addCreationPanel(pane);
}

private void addCreationPanel(Container pane) {
    JPanel infoPanel = new JPanel();
    infoPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.ipady = 5;
    JButton createDoc = new JButton("Create PDF");
    createDoc.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            JDialog dlg = new JDialog(frame, "Progress Dialog", true);
            progressBar = new JProgressBar(0, 500);
            progressBar.setOpaque(true);
            dlg.add(BorderLayout.CENTER, progressBar);
            dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));

            dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dlg.setSize(300, 75);
            dlg.setLocationRelativeTo(frame);
            dlg.setVisible(true);

            Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    while (progress < 500) {
                        progressBar.setValue(progress);
                        progress++;
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            });
            t.start();
        }
    });

    infoPanel.add(createDoc, c);
    pane.add(infoPanel);
}

When I run the program and click the createDoc button, the progress bar is not updated in the dialog, but if I close the dialog and click the button again, the progress bar is updating. I know it's something related with event dispatch thread, but I don't know how to change my code in order to always update the bar.

I've also tried with a SwingWorker, without success.

mawus
  • 1,178
  • 1
  • 11
  • 25

1 Answers1

0

Make JDialog visible after starting the thread.

t.start();
dlg.setVisible(true);

Use Swing Timer instead of Java Timer that is more suitable with Swing application.

Read more How to Use Swing Timers

Braj
  • 46,415
  • 5
  • 60
  • 76
  • Thanks for suggestion, but this time, the progress bar is updating only when I press the button for the first time... – mawus Jun 09 '14 at 19:08
  • It means it's working on first click as well that was your original issue. – Braj Jun 09 '14 at 19:09
  • 1
    The progress bar should work everytime I press the button. My first issue was that it wasn't updating on the first click. Now it's working only on first click – mawus Jun 09 '14 at 19:11
  • OK now move to next issue and look at the suggestion as well that I updated in my post. Can you try it yourself first? So you want to start it from the beginning every time when button is clicked? – Braj Jun 09 '14 at 19:12
  • 1
    set the `progress=0;` in the `actionPerformed()` method to start it from the beginning. – Braj Jun 09 '14 at 19:14