Bitcoin

Bitcoin
Bitcoin

JProgressBar Java

A progress bar is a widget that displays progress of a lengthy task, for instance file download or transfer. To create a progress bar in swing you use the JProgressbar class. With JProgressBar you can either create vertical or horizontal progress bar. In addition  JProgressBar class allows you to create another kind of progress bar which is known as indeterminate progress bar. The indeterminate progress bar is used to display progress with unknown progress or the progress cannot express by percentage.
There are five different constructors for JProgressBar:

Field

Following are the fields for javax.swing.JProgressBar class −
  • protected ChangeEvent changeEvent − Only one ChangeEvent is needed per instance since the event's only interesting property is the immutable source, which is the progress bar.
  • protected ChangeListener changeListener − Listens for change events sent by the progress bar's model, redispatching them to change-event listeners registered upon this progress bar.
  • protected BoundedRangeModel model − The object that holds the data for the progress bar.
  • protected int orientation − Whether the progress bar is horizontal or vertical.
  • protected boolean paintBorder − Whether to display a border around the progress bar.
  • protected boolean paintString − Whether to display a string of text on the progress bar.
  • protected String progressString − An optional string that can be displayed on the progress bar.

Class Constructors

Sr.No.Constructor & Description
1
JProgressBar()
Creates a horizontal progress bar that displays a border but no progress string.
2
JProgressBar(BoundedRangeModel newModel)
Creates a horizontal progress bar that uses the specified model to hold the progress bar's data.
3
JProgressBar(int orient)
Creates a progress bar with the specified orientation, which can be either SwingConstants. VERTICAL or SwingConstants.HORIZONTAL.
4
JProgressBar(int min, int max)
Creates a horizontal progress bar with the specified minimum and maximum.
5
JProgressBar(int orient, int min, int max)
Creates a progress bar using the specified orientation, minimum, and maximum.

Class Methods

Sr.No.Method & Description
1
void addChangeListener(ChangeListener l)
Adds the specified ChangeListener to the progress bar.
2
protected ChangeListener createChangeListener()
Subclasses that want to handle change events from the model differently can override this to return an instance of a custom ChangeListener implementation.
3
protected void fireStateChanged()
Send a ChangeEvent, whose source is JProgressBar, to all ChangeListeners that have registered interest in ChangeEvents.
4
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JProgressBar.
5
ChangeListener[] getChangeListeners()
Returns an array of all the ChangeListeners added to this progress bar with addChangeListener.
6
int getMaximum()
Returns the progress bar's maximum value from the BoundedRangeModel.
7
int getMinimum()
Returns the progress bar's minimum value from the BoundedRangeModel.
8
BoundedRangeModel getModel()
Returns the data model used by this progress bar.
9
int getOrientation()
Returns SwingConstants.VERTICAL or SwingConstants.HORIZONTAL, depending on the orientation of the progress bar.
10
double getPercentComplete()
Returns the percent complete for the progress bar.
11
String getString()
Returns a String representation of the current progress.
12
ProgressBarUI getUI()
Returns the look-and-feel object that renders this component.
13
String getUIClassID()
Returns the name of the look-and-feel class that renders this component.
14
int getValue()
Returns the progress bar's current value from the BoundedRangeModel.
15
boolean isBorderPainted()
Returns the borderPainted property.
16
boolean isIndeterminate()
Returns the value of the indeterminate property.
17
boolean isStringPainted()
Returns the value of the stringPainted property.
18
protected void paintBorder(Graphics g)
Paints the progress bar's border if the borderPainted property is true.
19
protected String paramString()
Returns a string representation of this JProgressBar.
20
void removeChangeListener(ChangeListener l)
Removes a ChangeListener from the progress bar.
21
void setBorderPainted(boolean b)
Sets the borderPainted property, which is true if the progress bar should paint its border.
22
void setIndeterminate(boolean newValue)
Sets the indeterminate property of the progress bar, which determines whether the progress bar is in determinate or indeterminate mode.
23
void setMaximum(int n)
Sets the progress bar's maximum value (stored in the progress bar's data model) to n.
24
void setMinimum(int n)
Sets the progress bar's minimum value (stored in the progress bar's data model) to n.
25
void setModel(BoundedRangeModel newModel)
Sets the data model used by the JProgressBar.
26
void setOrientation(int newOrientation)
Sets the progress bar's orientation to newOrientation, which must be SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
27
void setString(String s)
Sets the value of the progress string.
28
void setStringPainted(boolean b)
Sets the value of the stringPainted property, which determines whether the progress bar should render a progress string.
29
void setUI(ProgressBarUI ui)
Sets the look-and-feel object that renders this component.
30
void setValue(int n)
Sets the progress bar's current value to n.
31
void updateUI()
Resets the UI property to a value from the current look and feel.

Methods Inherited

This class inherits methods from the following classes −
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object
public JProgressBar()
JProgressBar aJProgressBar = new JProgressBar();

public JProgressBar(int orientation)
JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
JProgressBar bJProgressBar = new JProgressBar(JProgressBar.HORIZONTAL);

public JProgressBar(int minimum, int maximum)
JProgressBar aJProgressBar = new JProgressBar(0, 500);

public JProgressBar(int orientation, int minimum, int maximum)
JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL, 0, 1000);

public JProgressBar(BoundedRangeModel model)
// Data model, initial value 0, range 0-250, and extent of 0
DefaultBoundedRangeModel model = new DefaultBoundedRangeModel(0, 0, 0, 250);
JProgressBar aJProgressBar = new JProgressBar(model);
//An example on JProgressBar
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class JProgressBarSetValue extends JFrame {
  JProgressBar bar = new JProgressBar();
  JButton step = new JButton("Step");

  public JProgressBarSetValue() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    step.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int value = bar.getValue() + 7;
        if (value > bar.getMaximum()) {
          value = bar.getMaximum();
        }
        bar.setValue(value);
      }
    });

    getContentPane().add(bar, BorderLayout.NORTH);
    getContentPane().add(step, BorderLayout.EAST);
    pack();
    setVisible(true);
  }

  public static void main(String arg[]) {
    new JProgressBarSetValue();
  }
}
The Output

import javax.swing.*;    
public class ProgressBarExample extends JFrame{    
    JProgressBar jb;    
    int i=0,num=0;     
    ProgressBarExample(){    
    jb=new JProgressBar(0,2000);    
    jb.setBounds(40,40,160,30);         
    jb.setValue(0);    
    jb.setStringPainted(true);    
    add(jb);    
    setSize(250,150);    
    setLayout(null);    
}    
public void iterate(){    
    while(i<=2000){    
       jb.setValue(i);    
       i=i+20;    
       try{Thread.sleep(150);}catch(Exception e){}    
    }    
}    
public static void main(String[] args) {    
        ProgressBarExample m=new ProgressBarExample();    
        m.setVisible(true);    
        m.iterate();    
    }    
}
The Output is:
//ANOTHER JPROGRESSBAR PROGRAM
package jprogressbardemo;
import java.awt.*;
import javax.swing.*;
public class Main {
    public static void main(String[] args) {
        final int MAX = 100;
        final JFrame frame = new JFrame("JProgress Demo");
        // creates progress bar
        final JProgressBar pb = new JProgressBar();
        pb.setMinimum(0);
        pb.setMaximum(MAX);
        pb.setStringPainted(true);
        // add progress bar
        frame.setLayout(new FlowLayout());
        frame.getContentPane().add(pb);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);
        // update progressbar
        for (int i = 0; i <= MAX; i++) {
            final int currentValue = i;
            try {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        pb.setValue(currentValue);
                    }
                });
                java.lang.Thread.sleep(100);
            } catch (InterruptedException e) {
                JOptionPane.showMessageDialog(frame, e.getMessage());
            }
        }
    }
}
THE OUTPUT IS:
//ANOTHER JPROGRESSBAR CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class JProgressBarDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   
   public JProgressBarDemo(){      prepareGUI();
   }
   public static void main(String[] args){
      JProgressBarDemo  jbDemo = new JProgressBarDemo();      
      jbDemo.showProgressBarDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private JProgressBar progressBar;
   private Task task;
   private JButton startButton;
   private JTextArea outputTextArea;
   
   private void showProgressBarDemo(){
      headerLabel.setText("Control in action: JProgressBar"); 
      progressBar = new JProgressBar(0, 100);
      progressBar.setValue(0);
      progressBar.setStringPainted(true);
      startButton = new JButton("Start");
      outputTextArea = new JTextArea("",5,20);
      JScrollPane scrollPane = new JScrollPane(outputTextArea);    
      
      startButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            task = new Task();                
            task.start();
         }
      });
      controlPanel.add(startButton);
      controlPanel.add(progressBar);
      controlPanel.add(scrollPane);
      mainFrame.setVisible(true);  
   }
   private class Task extends Thread {    
      public Task(){
      }
      public void run(){
         for(int i =0; i<= 100; i+=10){
            final int progress = i;
            
            SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                  progressBar.setValue(progress);
                  outputTextArea.setText(outputTextArea.getText() 
                     + String.format("Completed %d%% of task.\n", progress));
               }
            });
            try {
               Thread.sleep(100);
            } catch (InterruptedException e) {}
         }
      }
   }   
}
THE OUTPUT IS:

No comments:

Post a Comment

Facebook