Search code examples
javaswingjprogressbar

Display Result only after JProgress Bar Completes 100% in Java


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%)??


Solution

  • 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.