Monday, 14 September 2015

JProgressBar class

JProgressBar class

The JProgressBar class is used to display the progress of the task.

Commonly used Constructors of JProgressBar class

  • JProgressBar(): is used to create a horizontal progress bar but no string text.
  • JProgressBar(int min, int max): is used to create a horizontal progress bar with the specified minimum and maximum value.
  • JProgressBar(int orient): is used to create a progress bar with the specified orientation, it can be either Vertical or Horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants.
  • JProgressBar(int orient, int min, int max): is used to create a progress bar with the specified orientation, minimum and maximum value.

Commonly used methods of JProgressBar class

1) public void setStringPainted(boolean b): is used to determine whether string should be displayed.
2) public void setString(String s): is used to set value to the progress string.
3) public void setOrientation(int orientation): is used to set the orientation, it may be either vertical or horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants..
4) public void setValue(int value): is used to set the current value on the progress bar.

Example of JProgressBar class

  1. import javax.swing.*;  
  2. public class MyProgress extends JFrame{  
  3. JProgressBar jb;  
  4. int i=0,num=0;  
  5.   
  6. MyProgress(){  
  7. jb=new JProgressBar(0,2000);  
  8. jb.setBounds(40,40,200,30);  
  9.       
  10. jb.setValue(0);  
  11. jb.setStringPainted(true);  
  12.       
  13. add(jb);  
  14. setSize(400,400);  
  15. setLayout(null);  
  16. }  
  17.   
  18. public void iterate(){  
  19. while(i<=2000){  
  20.   jb.setValue(i);  
  21.   i=i+20;  
  22.   try{Thread.sleep(150);}catch(Exception e){}  
  23. }  
  24. }  
  25. public static void main(String[] args) {  
  26.     MyProgress m=new MyProgress();  
  27.     m.setVisible(true);  
  28.     m.iterate();  
  29. }  
  30. }  

No comments:

Post a Comment