The following example illustrates creating JProgressBar. This example covers four constructors and important methods of the JProgressBar class.
import javax.swing.*;
import java.awt.*;
class JProgressBarExample extends JFrame
{
JProgressBar p1,p2,p3,p4;
public JProgressBarExample()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("JProgressBar Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
p1=new JProgressBar();
p2=new JProgressBar(SwingConstants.VERTICAL);
p3=new JProgressBar(10,90);
p4=new JProgressBar(SwingConstants.HORIZONTAL,10,50);
// Set value
p1.setValue(20);
p2.setValue(40);
p3.setValue(20);
p4.setValue(40);
// Set minimum,maximum
p1.setMinimum(10);
p1.setMaximum(40);
// If you set more than maximum, then
// maximum is considered, i.e.
// here setValue(40) is substituted
p1.setValue(42);
System.out.println("Value of p1 is "+p1.getValue());
// Set foreground
p1.setForeground(Color.RED);
p2.setForeground(Color.LIGHT_GRAY);
// Set string painted
// default string is <percent>%
p1.setStringPainted(true);
p2.setStringPainted(true);
p3.setStringPainted(true);
// Set custom string
p3.setString("Processing..");
// Set orientation
p4.setOrientation(SwingConstants.VERTICAL);
// Set indeterminate, means don't know
// how much process was done
p4.setIndeterminate(true);
// Set border
p1.setBorder(BorderFactory.createLineBorder(Color.RED,2,false));
// Get value and percent complete
// percent complete is out of 1 (between 0.0 - 1.0)
System.out.println("Percent complete "+p1.getPercentComplete());
add(p1);
add(p2);
add(p3);
add(p4);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
new JProgressBarExample();
}
}
No comments:
Post a Comment