This article describes the implementation of progress monitoring functions of Java Swing components. Share it for your reference, as follows:
Example 1:
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ProgressMonitor;import javax.swing.Timer;public class TestProgressMonitor { Timer timer; public void init() { final SimulatedTargetMi target = new SimulatedTargetMi(1000); // Execute a time-consuming task by starting a thread final Thread targetThread = new Thread(target); targetThread.start(); // Create a progress dialog final ProgressMonitor dialog = new ProgressMonitor(null, "Waiting for the task to complete, please do not close the window before the task is completed, otherwise the current operation will be cancelled...", "Completed: 0.00%", 0, target.getAmount()); // Create a timer timer = new Timer(300, new ActionListener() { public void actionPerformed(ActionEvent e) { // Set the completion ratio of the progress dialog with the current completion amount of the task dialog dialog.setProgress(target.getCurrent()); dialog.setNote("Completed:" + target.getPercent()); // If the user clicks the "Cancel" button in the progress dialog box if (dialog.isCanceled()) { // Stop the timer timer.stop(); // interrupt the task execution thread targetThread.interrupt(); // System exit System.exit(0); } } }); timer.start(); } public static void main(String[] args) { new TestProgressMonitor().init(); }}// Simulate a time-consuming task class SimulatedTargetMi implements Runnable { // The current completion of the task private volatile int current; // Total task volume private int amount; public SimulatedTargetMi(int amount) { current = 0; this.amount = amount; } public int getAmount() { return amount; } public int getCurrent() { return current; } // The run method represents the process of continuously completing tasks public void run() { while (current < amount) { try { Thread.sleep(50); } catch (InterruptedException e) { } current++; } } public String getPercent() { return String.format("%.2f", 100.0 * current / amount) + "%"; }}Running effect:
Example 2:
import java.awt.FlowLayout;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JProgressBar;import javax.swing.Timer; public class TestJProgressBar { JFrame frame = new JFrame("www.VeVB.COM - Current progress indication..."); // Create a vertical progress bar JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL); JLabel tipLabel = new JLabel("Tip:", JLabel.LEFT); JLabel contentLabel = new JLabel("Please do not close the window before the task is completed, otherwise the current operation will be cancelled...", JLabel.LEFT); JLabel statusLabel = new JLabel(" ", JLabel.CENTER); public void init() { frame.setLayout(new FlowLayout()); frame.setResizable(false); tipLabel.setFont(new Font("Serif", Font.PLAIN, 14)); contentLabel.setFont(new Font("Serif", Font.PLAIN, 14)); statusLabel.setFont(new Font("Serif", Font.PLAIN, 14)); JPanel panel = new JPanel(); // fr5.setBorder(new TitledBorder("BoxLayout - Y")); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(tipLabel); panel.add(Box.createVerticalStrut(2)); panel.add(contentLabel); panel.add(Box.createVerticalStrut(7)); panel.add(bar); // panel.add(Box.createVerticalGlue()); panel.add(Box.createVerticalStrut(2)); panel.add(statusLabel); frame.add(panel, 0); final SimulatedTarget target = new SimulatedTarget(1000); // Execute a time-consuming task by starting a thread final Thread thread = new Thread(target); thread.start(); // Set the percentage of drawing completion in the progress bar bar bar.setStringPainted(true); // bar.setPreferredSize(new Dimension(100, 18)); // Set the maximum and minimum values of the progress bar, bar.setMinimum(0); // Use the total task as the maximum value of the progress bar bar.setMaximum(target.getAmount()); final Timer timer = new Timer(300, new ActionListener() { public void actionPerformed(ActionEvent e) { // Set the value of the progress bar bar with the current completion amount of the task bar bar bar.setValue(target.getCurrent()); if (target.getAmount() <= target.getCurrent()) { statusLabel.setText("processing is completed,oh yes!"); } } }); timer.start(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { thread.interrupt(); timer.stop(); // System exit System.exit(0); } }); // This code sets the window size according to the placed components so that it can accommodate all the components you placed frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new TestJProgressBar().init(); }} // Simulate a time-consuming task class SimulatedTarget implements Runnable { // The current completion amount of the task private volatile int current; // Total task volume private int amount; public SimulatedTarget(int amount) { current = 0; this.amount = amount; } public int getAmount() { return amount; } public int getCurrent() { return current; } // The run method represents the process of continuously completing tasks public void run() { while (current < amount) { try { Thread.sleep(20); } catch (InterruptedException e) { } current++; } } public String getPercent() { return String.format("%.1f", 100.0 * current / amount) + "%"; }}Running results:
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.