📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Check out the complete Swing tutorial at https://www.javaguides.net/p/java-swing-tutorial.html.
Java Swing Progress Bar Example
package net.sourcecodeexamples.swingexample.components2;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static javax.swing.GroupLayout.Alignment.CENTER;
public class ProgressBarExample extends JFrame {
private static final long serialVersionUID = 1L;
private Timer timer;
private JProgressBar progBar;
private JButton startBtn;
private final int MAX_VAL = 100;
private void initializeUI() {
progBar = new JProgressBar();
progBar.setStringPainted(true);
startBtn = new JButton("Start");
startBtn.addActionListener(new ClickAction());
timer = new Timer(50, new UpdateBarListener());
createLayout(progBar, startBtn);
setSize(350, 150);
setTitle("JProgressBar");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void createLayout(JComponent...arg) {
JPanel pane = (JPanel) getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addComponent(arg[1])
);
gl.setVerticalGroup(gl.createParallelGroup(CENTER)
.addComponent(arg[0])
.addComponent(arg[1])
);
pack();
}
private class UpdateBarListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int val = progBar.getValue();
if (val >= MAX_VAL) {
timer.stop();
startBtn.setText("End");
return;
}
progBar.setValue(++val);
}
}
private class ClickAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
if (timer.isRunning()) {
timer.stop();
startBtn.setText("Start");
} else if (!"End".equals(startBtn.getText())) {
timer.start();
startBtn.setText("Stop");
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
ProgressBarExample progressBarExample = new ProgressBarExample();
progressBarExample.initializeUI();
progressBarExample.setVisible(true);
});
}
}
progBar = new JProgressBar();
progBar.setStringPainted(true);
timer = new Timer(50, new UpdateBarListener());
private class UpdateBarListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int val = progBar.getValue();
if (val >= MAX_VAL) {
timer.stop();
startBtn.setText("End");
return;
}
progBar.setValue(++val);
}
}
private class ClickAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
if (timer.isRunning()) {
timer.stop();
startBtn.setText("Start");
} else if (!"End".equals(startBtn.getText())) {
timer.start();
startBtn.setText("Stop");
}
}
}
Output
Check out the complete Swing tutorial at https://www.javaguides.net/p/java-swing-tutorial.html.
Related Swing Examples
- Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
- Swing ToolTip Tutorial with Example - In this tutorial, we will learn how to add tooltip text to a Swing component.
- Java Swing BorderLayout Example - In this example, we will learn how to use BorderLayout in GUI/swing based applications.
- Java Swing GridLayout Tutorial with Examples - In this tutorial, we will learn how to use GridLayout in GUI/swing based applications.
- Swing Mouse Move Events using MouseMotionAdapter - In this tutorial, we will learn how to receive mouse motion events using MouseMotionAdapter.
- Java Swing CheckBox Example - In this post, I show you how to use JCheckBox class to create a Radio button in a Swing-based application.
- Java Swing Radio Button Example - In this post, I show you how to use JRadioButton class to create a Radio button in a Swing-based application.
- Java Swing Progress Bar Example - In this post, I show you how to create a progress bar using the JProgressBar component in swing-based applications.
- Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
- Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.
- Java Swing Toggle Button Example - In this post, we will learn how to create a toggle button using the JToggleButton component in swing-based applications.
Comments
Post a Comment
Leave Comment