Create a Vertical ProgressBar in Java



In this article, we will learn about creating a vertical progress bar using JProgressBar.VERTICAL in Java. Progress bars are essential for tracking task completion in GUI applications, and a vertical orientation can add a unique touch to your design.

What is a Progress Bar in Java Swing?

A progress bar is a visual component in Java Swing that indicates the progress of a task. It can be horizontal or vertical, depending on the application's design requirements. Java Swing provides the JProgressBar class to create and manage progress bars efficiently.

Features of a Vertical Progress Bar

The following are features of the vertical progress bar in Java Swing ?

  • Orientation: The JProgressBar.VERTICAL constant sets the progress bar to a vertical orientation.
  • Range: The progress bar has a range defined by minimum and maximum values.
  • Customizable Appearance: You can set colors, bounds, and display text.

Creating a vertical ProgressBar

Following are the steps to create a vertical Progress bar in Java ?

  • Create JFrame: Extend JFrame and declare a vertical JProgressBar.
  • Customize Progress Bar: Set its position, and size, and enable percentage display.
  • Add to Frame: Add the progress bar to the frame and configure frame settings.
  • Update Progress: Increment progress in a loop using setValue() with delays (Thread.sleep).
  • Run Application: Instantiate the frame, make it visible, and call the increment method.

To create a vertical ProgressBar, use the following property ?

JProgressBar.VERTICAL

Creating the Progress Bar ?

progressBar = new JProgressBar(JProgressBar.VERTICAL, 0, 1000);

Java Program to create a vertical ProgressBar

The following is an example of creating a vertical ProgressBar ?

package my;
import java.awt.Color;
import javax.swing.*;
public class SwingDemo extends JFrame {
   JProgressBar progressBar;
   int i = 0;
   SwingDemo() {
      progressBar = new JProgressBar(JProgressBar.VERTICAL,0, 1000);
      progressBar.setBounds(70, 50, 120, 30);
      progressBar.setValue(0);
      progressBar.setStringPainted(true);
      add(progressBar);
      setSize(550, 150);
      setLayout(null);
   }
   public void inc() {
      while (i <= 1000) {
         progressBar.setValue(i);
         i = i + 50;
         try {
            Thread.sleep(100);
         } catch (Exception e) {}
      }
   }
   public static void main(String[] args) {
      SwingDemo s = new SwingDemo();
      s.setVisible(true);
      s.inc();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-24T17:55:32+05:30

469 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements