0% found this document useful (0 votes)
6 views2 pages

Progress Bar Example

This Java program demonstrates a simple GUI application using Swing that features a progress bar and a stop button. The progress bar updates in a background thread, simulating work by incrementing its value every 100 milliseconds. The user can stop the progress by clicking the stop button, which sets a flag to halt the updating loop.

Uploaded by

louisserivera27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Progress Bar Example

This Java program demonstrates a simple GUI application using Swing that features a progress bar and a stop button. The progress bar updates in a background thread, simulating work by incrementing its value every 100 milliseconds. The user can stop the progress by clicking the stop button, which sets a flag to halt the updating loop.

Uploaded by

louisserivera27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

FlowLayout;
import [Link];
import [Link].*;

public class ProgressBarExample {


private static volatile boolean running = true; // Flag to control progress

public static void main(String[] args) {


final int MAXIMUM = 100;
final JFrame jFrame = new JFrame("JProgress Demo ");

// Create Frame
[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);

// ProgressBar
JProgressBar progressBar = new JProgressBar(0, MAXIMUM);
[Link](true);
[Link](progressBar);

// Stop Button
JButton button = new JButton("Stop");
[Link](button);

[Link](true);

// Background Thread to Update ProgressBar


Thread progressThread = new Thread(() -> {
for (int i = 0; i <= MAXIMUM && running; i++) {
try {
[Link](100); // Simulate work
final int progress = i;
[Link](() -> [Link](progress));
} catch (InterruptedException e) {
[Link](jFrame, "Process Interrupted: " + [Link]());
break;
}
}
});
[Link]();

// Stop Button Action (Stops the Loop)


[Link]((ActionEvent e) -> {
running = false; // Stop the loop
});
}
}

You might also like