/* * ================================================================== * DemoProgressMonitor.java : This applet computes the sum of numbers * from one to a specified number. A progress bar pops up after the * specified time (5 milliseconds in the code) to indicate progress * in the calculation. * * Adapted from : Pantham S., Pure JFC Swing, 1999. * Modified by : Mark Austin March, 2001 * ================================================================== */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class DemoProgressMonitor extends JApplet { Container container = null; JButton startButton, stopButton; JTextField inputTextField, outputTextField; ProgressMonitor pMonitor = null; Timer timer = null; static int sum = 0; static int counter = 0; public void init() { // 1. Get the handle on the content pane and // assign the grid layout. container = this.getContentPane(); container.setLayout(new GridLayout(2,1)); // 2. Add a horizontal box to the container. Box hbox1 = Box.createHorizontalBox(); container.add(hbox1); // 3. Add labels and input and output text fields // to the horizontal box. hbox1.add(Box.createHorizontalGlue()); JLabel label1 = new JLabel("Sum of first ", JLabel.LEFT); label1.setFont(new Font("Dialog", Font.PLAIN, 15)); hbox1.add(label1); inputTextField = new JTextField("100", 4); hbox1.add(inputTextField); JLabel label2 = new JLabel(" numbers is ", JLabel.LEFT); label2.setFont(new Font("Dialog", Font.PLAIN, 15)); hbox1.add(label2); outputTextField = new JTextField(10); hbox1.add(outputTextField); hbox1.add(Box.createHorizontalGlue()); // 4. Add another horizontal box to the container. Box hbox2 = Box.createHorizontalBox(); container.add(hbox2); // 5. Add Start and Stop buttons to the container. startButton = new JButton("Start"); startButton.addActionListener(new ButtonListener()); hbox2.add(Box.createHorizontalGlue()); hbox2.add(startButton); hbox2.add(Box.createHorizontalGlue()); stopButton = new JButton("Stop"); stopButton.addActionListener(new ButtonListener()); hbox2.add(Box.createHorizontalGlue()); hbox2.add(stopButton); hbox2.add(Box.createHorizontalGlue()); // 6. Create a timer object. timer = new Timer(0, new TimerListener()); } // 7. Timer listener that computes the sum of natural numbers, // indicates the computation progress, and displays the result. class TimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (Integer.parseInt(inputTextField.getText())> 0){ counter++; sum = sum+counter; pMonitor.setProgress(counter); pMonitor.setNote("Currently Adding " + counter); outputTextField.setText(Integer.toString(sum)); } else { outputTextField.setText("0"); } if (counter >= Integer.parseInt(inputTextField.getText())){ timer.stop(); startButton.setEnabled(true); } } } // 8. Button listener that actually starts or stops the process. class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); if (button.getText() == "Start") { startButton.setEnabled(false); //9. Create a progress monitor. pMonitor = new ProgressMonitor(container, "Computation in Progress...", "Note", 0, 100); pMonitor.setMillisToPopup( 5 ); outputTextField.setText(""); if (inputTextField.getText() != " ") { pMonitor.setMaximum(Integer.parseInt( inputTextField.getText())); sum = 0; counter = 0; timer.start(); } } else if (button.getText() == "Stop") { startButton.setEnabled(true); timer.stop(); pMonitor.close(); outputTextField.setText(""); sum = 0; counter = 0; } } } }