Solved

jProgressBar.setIndeterminate(true);

Posted on 2011-03-06
9
1,144 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 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

16 Experts available now in Live!

Get 1:1 Help Now