Solved

jProgressBar.setIndeterminate(true);

Posted on 2011-03-06
9
1,157 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
 
LVL 86

Accepted Solution

by:
CEHJ earned 350 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Assisted Solution

by:objects
objects earned 150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now