MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.Timer;

public class MainClass {
    static ProgressMonitor monitor;

    static int progress;

    static Timer timer;

    static class ProgressMonitorHandler implements ActionListener {
        public void actionPerformed(ActionEvent actionEvent) {
            if (monitor == null)
                return;
            if (monitor.isCanceled()) {
                System.out.println("Monitor canceled");
                timer.stop();
            } else {
                progress += 3;
                monitor.setProgress(progress);
                monitor.setNote("Loaded " + progress + " files");
            }
        }
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame("ProgressMonitor Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton autoIncreaseButton = new JButton("Automatic Increase");
        ActionListener autoIncreaseActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {

                Component parent = (Component) actionEvent.getSource();
                monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
                progress = 0;

                if (monitor != null) {
                    if (timer == null) {
                        timer = new Timer(250, new ProgressMonitorHandler());
                    }
                    timer.start();
                }
            }
        };
        autoIncreaseButton.addActionListener(autoIncreaseActionListener);
        frame.add(autoIncreaseButton);

        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}