Solved

jProgressBar.setIndeterminate(true);

Posted on 2011-03-06
9
1,236 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:
Suggested Courses

630 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