TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Java Swing

    Create Progress Bar using JProgressBar

    Overview

    In this tutorial, we will learn steps to create a progress Bar using JProgressBar class. Using JProgressBar we can create a vertical or a horizontal progress bar.

    Create Progress Bar

    A progress bar is a GUI component that is used to display the progress of a task. For example, the progress of a file download, file copy, file upload, etc.

    To import the class in the java program, we can use the following import statement.

    import javax.swing.JProgressBar;

    To create a progress bar we can use the JProgressBar class.

    JProgressBar pBar = new JProgressBar();

    Java Program

    In this Java program, we will use JProgressBar class to create a simple progress bar.

    package com.testingdocs.swing.components;
    
    /**********************************************
     * FileName: ProgressBarDemo.java
     * Package : com.testingdocs.swing.components
     * 
     * Java Tutorials - www.TestingDocs.com
     **********************************************/
    
    import java.awt.*;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JProgressBar;
    import javax.swing.SwingUtilities;
    
    
    public class ProgressBarDemo {
    
     public static void main(String[] args) {
     final int MAX_PERCENT = 100;
     JFrame frame = new JFrame("JProgress -www.TestingDocs.com");
    
     // Create Progress Bar
     JProgressBar pBar = new JProgressBar();
     pBar.setMinimum(0);
     pBar.setMaximum(MAX_PERCENT);
     pBar.setStringPainted(true);
    
     // Add Progress Bar
     frame.setLayout(new FlowLayout());
     frame.getContentPane().add(pBar);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(500, 500);
     frame.setVisible(true);
    
     // Update Progress Bar
     for (int i = 0; i <= MAX_PERCENT; i++) {
     final int currentValue = i;
     try {
     SwingUtilities.invokeLater(new Runnable() {
     public void run() {
     pBar.setValue(currentValue);
     }
     });
     java.lang.Thread.sleep(1000);
     } catch (InterruptedException e) {
     JOptionPane.showMessageDialog(frame, e.getMessage());
     }
     }
     }
    }

    Output

    Run the program to view the output.

    Create Progress bar

    —

    Java Tutorial on this website:

    https://www.testingdocs.com/java-tutorial/

    For more information on Java, visit the official website :

    https://www.oracle.com/in/java/

     

    Related Posts

    Add GUI Components Swing

    Java Swing /

    Add GUI Components to a Swing Container

    JPanel Container Class

    Java Swing /

    Swing JPanel Container Class

    Swing CardLayout

    Java Swing /

    Java Swing CardLayout

    Java Swing GridLayout

    Java Swing /

    Java Swing GridLayout

    Java Swing FlowLayout

    Java Swing /

    Java Swing FlowLayout

    ‹ Create Lists using JList Class› Create a Slider using JSlider Class

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com