1

I have a Jbutton "Highlight" that when clicked calls the Jprogress Bar. The progresss bar works fine till 100%.

How can I display my Result only after Progress Bar has reached 100%.

Here is part of the code:

final JProgressBar progressBar = new JProgressBar();
progressBar.setMaximum(100);
progressBar.setStringPainted(true);


btnNewButton_2.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent e) {


                Thread t = new Thread(new Runnable() {
                    public void run() {
                    int i = 1;
                    progressBar.setMinimum(0);
                    progressBar.setMaximum(100);
                    try {
                        while (i <= 100 || true) {
                            progressBar.setValue(i);
                            i++;
                            Thread.sleep(38);

                        }
                    } catch (InterruptedException ex){}

            }});
            t.start();

//Where should I put System.out.println("Jprogress Bar Reached 100%)??

RudolphEst
  • 1,240
  • 13
  • 21
Ms_Joe
  • 160
  • 2
  • 7
  • 16

2 Answers2

1

Add if statement after i++ :)

  i++;
  if (i == 100) {
    displayResult();
    return;
  }

Do not forget to use invokeAndWait. The current code as is most likely will not run well as you are manipulating GUI controls from the wrong thread.

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • I do not see why there is a need for invokeAndWait here... The whole point is that you do not want your GUI to wait (hang) while the background thread is working. We do not need to worry about what is going on in the AWT event queue here. It is only important to remember to fire GUI events on the AWT thread, but you are allowed to invoke the getters and setters for the GUI components from any thread. – RudolphEst Feb 28 '13 at 16:37
  • 1
    @RudolphEst There shouldn't be any modification of the GUI components from a different thread (which is what is happening in the example). – sdasdadas Feb 28 '13 at 18:26
  • @sdasdadas As I understand the it, you should not influence GUI events in any thread but the `AWTEventQueue`. In this case setting the value will fire the actual change event on the `AWTEventQueue` not in your current thread, so the actual modification and update of the GUI will happen when that event is handled from the AWTEventQueue. If you really feel it is necessary you could wrap the `progressBar.setValue()` in an `invokeLater()`, but using `invokeAndWait()` is incorrect here, since the background process can continue without waiting for the GUI to update. – RudolphEst Mar 01 '13 at 11:36
  • 2
    @sdasdadas nowhere in the [documentation](http://docs.oracle.com/javase/6/docs/api/javax/swing/JProgressBar.html) is written that JProgressBar.setValue is multithread safe but instead it is written the whole class is not. Also from the [source code](http://javasourcecode.org/html/open-source/jdk/jdk-6u23/javax/swing/JProgressBar.java.html) does not look like the call is queued or somewhat. Swing is generally not multithread safe while this may work just by chance for certain program, OS and jre. – Audrius Meškauskas – Audrius Meškauskas Mar 01 '13 at 18:08
  • @AudriusMeškauskas Did you mean to address RudolphEst? I think we agree with each other. – sdasdadas Mar 01 '13 at 20:13
  • @sdasdadas I mean I support you. – Audrius Meškauskas Mar 02 '13 at 12:51
0
try{
    while (i <= 100 || true) {
        progressBar.setValue(i);
        i++;
        Thread.sleep(38);
     }
     // progress is definitely at 100 now
     // do whatever you need to display result here -- it would be faster than doing the check 
     // if(i==100) in every execution inside the while loop.
     System.out.println("Progress is done!!!");
} catch (....
RudolphEst
  • 1,240
  • 13
  • 21