Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jProgressBar.setIndeterminate(true);

Posted on 2011-03-06
9
Medium Priority
?
1,264 Views
Last Modified: 2012-08-13
I start an indeterminate JProgressBar like so:
jProgressBar.setIndeterminate(true);

Open in new window

then when the task is done, it turn it off:
jProgressBar.setIndeterminate(false);

Open in new window

When I go to turn it on again, by setting it to true, nothing happens.
Is there something I need to do to reinitialize the progress bar?

0
Comment
Question by:allelopath
  • 4
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35050933
are you setting the value back to its minimum?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35055334
Make sure whatever else you're doing (you probably ARE doing something else if you're using a progress bar) is not also running in the EDT. It should be running in its own thread (use a SwingWorker) or the second call to setIndeterminate probably won't get through
0
 
LVL 1

Author Comment

by:allelopath
ID: 35056759
Attached is a test program. When "Go" is clicked, I want the progress bar to "cycle", then after 2 seconds, it should stop.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;

public class ProgressPanel extends JPanel {

	JProgressBar jProgressBar;

	/**
	 * constructor
	 */
	public ProgressPanel() {

		this.setLayout(new BorderLayout());

		JButton button = new JButton("Go");
		this.add(button, BorderLayout.LINE_START);

		button.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e) {
					try {
						setProgressIndeterminate(true);
						Thread.sleep(2000);
						setProgressIndeterminate(false);
						//Thread.sleep(2000);
						//setProgressIndeterminate(true);
					} 
					catch (InterruptedException exception) {
						exception.printStackTrace();
					}
			  }
			});

		jProgressBar = new JProgressBar();
		this.setProgressIndeterminate(false);

		this.add(jProgressBar, BorderLayout.LINE_END);

	}

	/**
	 * 
	 * @param indeterminate
	 */
	public void setProgressIndeterminate(boolean indeterminate) {

		if (indeterminate) {
			System.out.println("ProgressPanel.setProgressIndeterminate(): true");
			jProgressBar.setIndeterminate(true);
		}
		else {
			System.out.println("ProgressPanel.setProgressIndeterminate(): false");
			jProgressBar.setIndeterminate(false);
			jProgressBar.setValue(0);
		}

		this.updateUI();
	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String args[]) {

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (final Exception e) {
			e.printStackTrace();
		}

		try {

			JFrame frame = new JFrame("This is a test");
			ProgressPanel progressPanel = new ProgressPanel();
			frame.add(progressPanel);
			frame.setSize(300, 100);

			frame.addWindowListener(new WindowListener() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}

				@Override
				public void windowActivated(WindowEvent arg0) {}

				@Override
				public void windowClosed(WindowEvent arg0) {}

				@Override
				public void windowDeactivated(WindowEvent arg0) {}

				@Override
				public void windowDeiconified(WindowEvent arg0) {}

				@Override
				public void windowIconified(WindowEvent arg0) {}

				@Override
				public void windowOpened(WindowEvent arg0) {	} 
			});

			frame.setVisible(true);


		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 1400 total points
ID: 35059880
You can simplify that a lot even while using SwingWorker
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;


public class ProgressPanel extends JPanel {
    JProgressBar jProgressBar;

    public ProgressPanel() {
	this.setLayout(new BorderLayout());
	jProgressBar = new JProgressBar();
	JButton button = new JButton("Go");
	this.add(button, BorderLayout.LINE_START);
	this.add(jProgressBar, BorderLayout.LINE_END);

	button.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		jProgressBar.setIndeterminate(true);
		new ProgressTask().execute();
	    }
	});
    }

    /**
     *
     * @param indeterminate
     */
    public void setProgressIndeterminate(boolean indeterminate) {
	if (indeterminate) {
	    System.out.println("ProgressPanel.setProgressIndeterminate(): true");
	    jProgressBar.setIndeterminate(true);
	} else {
	    System.out.println(
		    "ProgressPanel.setProgressIndeterminate(): false");
	    jProgressBar.setIndeterminate(false);
	    jProgressBar.setValue(0);
	}

	this.updateUI();
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
	try {
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (final Exception e) {
	    e.printStackTrace();
	}

	try {
	    JFrame frame = new JFrame("This is a test");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    ProgressPanel progressPanel = new ProgressPanel();
	    frame.add(progressPanel);
	    frame.setSize(300, 100);
	    frame.setVisible(true);
	} catch (final Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * constructor
     */
    private class ProgressTask extends SwingWorker<Void, Void> {
	public Void doInBackground() {
	    try {
		System.out.println("Sleeping for two secs...");
		Thread.sleep(2000);
	    } catch (InterruptedException e) {
		/* ignore */
	    }

	    return null;
	}

	public void done() {
	    jProgressBar.setIndeterminate(false);
	}
    }
}

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 600 total points
ID: 35061901
>                                     Thread.sleep(2000);

you can't do a sleep() inside the event dispatch thread
you should instead use a Timer

      button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
             setProgressIndeterminate(true);
               Timer timer = new Timer(2000, new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                        setProgressIndeterminate(true);
               });
               timer.start();
          }
      });

you'll also find a simple example of using a progress bar here
http://helpdesk.objects.com.au/java/simple-progress-bar-example
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35072599
:)
0
 
LVL 92

Expert Comment

by:objects
ID: 35073836
you don't need a SwingWorker (as shown by my example), its not what they are for
they are intended for doing background processing, which you have none.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35073987
It's a Timer you don't need ;) A progress bar is to show the progress of a task, not how many times a timer fires
0
 
LVL 92

Expert Comment

by:objects
ID: 35074048
Are you serious :D
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question