jProgressBar doesn't update as method runs

I have a button that processes a test function, when I press this button, I want a jProgressbar to popup and disable touching the original application. Essentially, the progressbar should only have focus.

my button:

    private JButton getRunSelectedTestsButton() {
        if (runSelectedTests == null) {
            runSelectedTests = new JButton();
            runSelectedTests.setText("Run Selected");
            runSelectedTests.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    Runnable processor = new Runnable() {
                        public void run() {
                            progressPop = getPopupBar();
                            progress = getTestProgress();
                            progressPop.setVisible(true);
                            progress.setVisible(true);
                            runTests();
                        }
                    };
                   begin = new Thread(processor);
                    begin.start();
                }
            });
        }
        return runSelectedTests;
    }

The progressbar is contained in a JDialog

    public JProgressBar getTestProgress() {
        progress = new JProgressBar();
        progress.setValue(0);
        progress.setVisible(true);
        progress.setStringPainted(true);
        progress.setString("Starting test run");
        return progress;
    }
   
    public JDialog getPopupBar() {
        progressPop = new JDialog();
        progressPop.setSize(300, 80);
        progressPop.setModal(true);
        progressPop.setTitle("Running Tests");
        progressPop.setLocation(jContentPane.getWidth() /2, jContentPane.getHeight() /2);
        JDialog.setDefaultLookAndFeelDecorated(true);
        progressPop.add(getTestProgress());
        return progressPop;
    }

When I click the Run Selected button, the jDialog with the progressbar pops up and locks off the lower window (Modal I assume), but it doesn't do my processing until I close the JProgressBar (Which stays at a zero value)

What am I doing wrong?
LVL 2
k41d3nAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
                   Runnable processor = new Runnable() {
                        public void run() {
                            EventQueue.invokeLater(new Runnable() {
                               public void run() {
                                 getPopupBar().setVisible(true);
                               }
                            });
                            runTests();
                        }
                    };
0
 
objectsCommented:
Used EventQueue.invokeLater() to pop up your dialog.
0
 
cjardCommented:
use a separate thread to control the progress bar. in java 1.5 you have ProgressBarManager to do this, because Sun realised that too many people didnt know you have to make a separate thread for a proress bar to work.. so many people thought java had a bug, when they just didnt understand how java repaints the display when it is busy (it doesnt; repainting is given very low priority)
0
 
k41d3nAuthor Commented:
Objects, it looks like your suggestion worked.

Thank you for that.
0
 
objectsCommented:
no worries :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.