Slide 1
Slide 2
Slide 3

Progress Bar in java programming (V81)

No Comments

 

JProgressBar

ProgressBar is a graphical control used in user interfaces to visually display the progress of a task that takes a certain amount of time to complete.

Example:

Source Code:

Copied!
    import java.awt.Color;
    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        // Create Swing Timer (update every 100ms)
        javax.swing.Timer timer = new javax.swing.Timer(100, (e) -> {
            int value = progressBar.getValue();
            if (value < 100) {
                progressBar.setUI(new javax.swing.plaf.basic.BasicProgressBarUI());
                progressBar.setStringPainted(true);
                    if (value <= 25) {
                    progressBar.setForeground(Color.YELLOW);
                    } else if (value <= 50) {
                        progressBar.setForeground(Color.ORANGE);
                    } else if (value <= 75) {
                        progressBar.setForeground(Color.PINK);
                    } else {
                        progressBar.setForeground(Color.RED);
                    }
                progressBar.setValue(value + 1);
                lblPercent.setText("Progress: " + (value + 1) + "%");
            } else {
                ((javax.swing.Timer) e.getSource()).stop();
                progressBar.setUI(new javax.swing.plaf.basic.BasicProgressBarUI());
                progressBar.setForeground(Color.GREEN);
                progressBar.setStringPainted(true);
                lblPercent.setText("Done!");
            }
        });

        timer.start();  // Start when form opens

    }                                                                                   

Result:


Watch the video:



Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top