How to create Vertical Progressbar in java swing?

Many times we need to create progress bar with veritcal orientation, its easy to create vertical JProgressBar.
JProgressBar have constructor JProgressBar(int orient) which creates a progress bar with the specified orientation, which can be either JProgressBar.VERTICAL or JProgressBar.HORIZONTAL , in the example below we are passing JProgressBar.VERTICAL to the JProgressBar to make progressbar vertical.


public class VerticleScrollBarExample extends JPanel {

  JProgressBar vertbar;

  static final int V_MINIMUM = 0;

  static final int V_MAXIMUM = 100;

  public VerticleScrollBarExample() {
    vertbar = new JProgressBar( JProgressBar.VERTICAL , V_MINIMUM,V_MAXIMUM );
    //Using JProgressbar.VERTICAL for creating vertical progress bar
    add(vertbar);
  }

  public void updateBar(int newValue) {
    vertbar.setValue(newValue);
  }

  public static void main(String args[]) {

    final VerticleScrollBarExample it = new VerticleScrollBarExample();

    JFrame frame = new JFrame("Vertical Progress Bar Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(it);
    frame.pack();
    frame.setVisible(true);

    for (int i = V_MINIMUM; i <= V_MAXIMUM; i++) {
      final int percent = i;
      try {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            it.updateBar(percent);
          }
        });
        java.lang.Thread.sleep(100);
      } catch (InterruptedException e) {
        ;
      }
    }
  }
}

                                                                                                                              

Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment