• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

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?
0
k41d3n
Asked:
k41d3n
  • 3
1 Solution
 
objectsCommented:
Used EventQueue.invokeLater() to pop up your dialog.
0
 
objectsCommented:
                   Runnable processor = new Runnable() {
                        public void run() {
                            EventQueue.invokeLater(new Runnable() {
                               public void run() {
                                 getPopupBar().setVisible(true);
                               }
                            });
                            runTests();
                        }
                    };
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now