Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jProgressBar.setIndeterminate(true);

Posted on 2011-03-06
9
Medium Priority
?
1,251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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 …
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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