Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a progressbar

Status
Not open for further replies.

Elsje

Programmer
Feb 20, 2004
44
BE
Hello,

I'm trying to build a class that shows a progressbar.
But when I want to try this he only shows me the frame with the progressbar, but it doesn't function like it should be.
This is just a test to see how a progressbar works.

In a later phase I want to implement this class in a program that can call this to show a progressbar. Is it possible to work with 2 frames. In the first frame I like to do some calculatens while these are busy I like to show the second frame with the progressbar. (or do I have to place the progressbar in the same frame)


Thanks in advance

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Progress extends JFrame{
public static JProgressBar progressBar;

public Progress(){

Dimension scrSize= Toolkit.getDefaultToolkit().getScreenSize();
this.setTitle("Vooruitgang.....");
this.setSize(250, 50);
this.setVisible(true);
this.setResizable(false);

this.setLocation((scrSize.width - 500)/2, (scrSize.height - 200)/2);
progressBar = new JProgressBar();

}

public static void main(String[] args) {
new Progress();

initProgressBar(25000);
for (int i=0; i<=25000; i+=1){
iterate(i);
}

}

public static void initProgressBar(int end){
progressBar.setMinimum(0);
progressBar.setMaximum(end);
progressBar.setValue(0);
progressBar.setString("0% completed");
progressBar.setStringPainted(true);
}

public static void iterate(int progress){
progressBar.setValue(progress);
progressBar.setString(progress + "%completed");
}
}
 
If you plan with more than one progressbar, using a static variable is a bad decision.
It's a common beginners-mistake, to get around the compiler-error-messages. Search the forum (and other ones) or better have a look into your books, regarding 'static'. You will need it, and it's not to be explained in few words.

Second: your main fault was to forget to add the progressbar to your frame - therefore it was never displayed.

Third: if you set the progress to 25000, you will get:
Code:
progressBar.setString (progress + "% completed");
25000% completed

Code:
public class Progress extends JFrame
{
	public JProgressBar progressBar;

	public Progress()
	{
		
		Dimension scrSize= Toolkit.getDefaultToolkit().getScreenSize();
		setTitle ("Vooruitgang.....");
		setSize (250, 50);
		setVisible (true);
		setResizable (false);
		
		setLocation ((scrSize.width - 500)/2, (scrSize.height - 200)/2);
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		progressBar = new JProgressBar();
		add (progressBar);
		initProgressBar(100);
		
		System.out.println ();
		for (int i=0; i<=100; i+=1)
		{
			iterate(i);
			try
			{
				Thread.sleep (20);
			} 
			catch (InterruptedException ignored) {}
			System.out.print (".");
			setTitle("Foogang..." + i);
		}
		System.out.println ();
	}

	public static void main(String[] args) 
	{
		new Progress();
	}
    
	public void initProgressBar (int end)
	{
		progressBar.setMinimum (0);
		progressBar.setMaximum (end);
		progressBar.setValue (0);
		progressBar.setString ("0% completed");
		progressBar.setStringPainted (true);
	}
    
	public void iterate (int progress)
	{
		progressBar.setValue (progress);
		progressBar.setString (progress + "%completed");
	}
}


dont't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top