JProgressBarSetValue.java Source code

Java tutorial

Introduction

Here is the source code for JProgressBarSetValue.java

Source

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class JProgressBarSetValue extends JFrame {
    JProgressBar bar = new JProgressBar();
    JButton step = new JButton("Step");

    public JProgressBarSetValue() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        step.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int value = bar.getValue() + 7;
                if (value > bar.getMaximum()) {
                    value = bar.getMaximum();
                }
                bar.setValue(value);
            }
        });

        getContentPane().add(bar, BorderLayout.NORTH);
        getContentPane().add(step, BorderLayout.EAST);
        pack();
        setVisible(true);
    }

    public static void main(String arg[]) {
        new JProgressBarSetValue();
    }
}