Saturday, August 24, 2013

JProgressBar

JProgressBar is a component to visually display the progress of a task. The progress bar displays the percentage of the task completed. It starts with an empty rectangle. Then the rectangle is filled in gradually toward the complete state of the task.

Constructors of the JProgressBar class:
-JProgressBar()
creates a progress bar.
-JProgressBar(BoundedRangeModel model)
creates a progress bar with its data are taken from the model.
-JProgressBar(int min, max)
creates a progress bar with the specified minimum and maximum values.
-JProgressBar(int orent,int min,int max)
creates a progress bar with the specified orientatin, minimum and maximum values.

Useful methods of the JProgressBar class:
-getValue():int
returns the current value of the progress bar.
-isIndeterminate():boolean
returns true if the progress bar is indeterminate state.
-setIndeterminate(boolean value):void
allows the progress bar in determinate or indeterminate state.
-setStringPainted(boolean painted):void
Shows or deos not show the string representing the percentage of the work progress.
-setValue(int value):void
sets the current value of the progress bar.

In the example program below, the progress bar is created to show the progress of retrieving available font names from the system. The DefaultRoundedRangleModel class is used to define the current value, extent value, an min and max values of the progress bar. The Thread.sleep(50) delays each progress step 50 milliseconds to ensure the progress is visible.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class JProgressBarShow extends JFrame{
JLabel lbl;
JProgressBarShow(String title){
setTitle(title);
setSize(500,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
setVisible(true);
JPanel p=new JPanel();
p.setLayout(new GridLayout(2,1));
BoundedRangeModel model=new DefaultBoundedRangeModel(0,1,0,100);
String[] fn=fnames();
model.setMinimum(0);
model.setMaximum(fn.length);
model.setExtent(1);
JProgressBar pbar=new JProgressBar(model);
pbar.setForeground(Color.BLUE);
pbar.setStringPainted(true);
p.add(pbar);
lbl=new JLabel("Please wait...");
lbl.setForeground(Color.RED);
p.add(lbl);
contpane.add(p, BorderLayout.NORTH);
validate();
DefaultListModel lmodel=new DefaultListModel();
for(String n:fn){
lmodel.addElement(n);
pbar.setValue(pbar.getValue()+1);
try{
Thread.sleep(50);
}catch(InterruptedException ie){}
}
lbl.setText("Complete");
JList fontlist=new JList(lmodel);
contpane.add(fontlist,BorderLayout.CENTER);
validate();

}

public String[] fnames(){
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
return ge.getAvailableFontFamilyNames();

}



}

public class JFrameSwing {
public static void main(String[] args){
new JProgressBarShow("JProgressBar");
}
}

JProgressBar task in progressing

JProgressBar complete task

No comments:

Post a Comment