Listing 17.1 Timer (TTimer.java)
/*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.*;
public class TTimer extends JApplet {
Container container = null;
Timer timer = null;
JLabel label = null;
JSlider slider1 = null;
JSlider slider2 = null;
Color[] color = {Color.blue, Color.green, Color.red,
Color.yellow, Color.lightGray};
public void init() {
// 1. Get the handle on the applet's content pane.
container = this.getContentPane();
// 2. Create a label and attach it to the applet.
label = new JLabel("Swing can flash!", JLabel.CENTER);
label.setBackground(Color.white);
label.setFont(new Font("Dialog", Font.BOLD, 40));
label.setOpaque(true);
container.add(label);
// 3. Create a horizontal box and attach it at the
// bottom portion of the content pane.
Box box = Box.createHorizontalBox();
container.add(box, BorderLayout.SOUTH);
// 4. Create a vertical box and add it to the horizontal box.
Box vbox1 = Box.createVerticalBox();
box.add(vbox1);
// 5. Create labels and sliders and attach them to
// a vertical box.
JLabel initDelay = new JLabel("Initial Delay", JLabel.CENTER);
initDelay.setPreferredSize(new Dimension(200, 25));
vbox1.add(initDelay);
slider1 = new JSlider(JSlider.HORIZONTAL, 0, 60000, 0);
slider1.addChangeListener(new SliderListener());
vbox1.add(slider1);
JLabel delay = new JLabel("Timer Delay", JLabel.CENTER);
delay.setPreferredSize(new Dimension(200, 25));
vbox1.add(delay);
slider2 = new JSlider(JSlider.HORIZONTAL, 0, 2000, 1000);
slider2.addChangeListener(new SliderListener());
vbox1.add(slider2);
// 6. Create another vertical box and add it to the
// horizontal box.
Box vbox2 = Box.createVerticalBox();
box.add(vbox2);
// 7. Create a Swing panel with grid layout and add it to
// the vertical box created in Snippet 6.
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,2,5,5));
vbox2.add(panel);
// 8. Create Start, Stop, and Restart buttons, and add them
// to the panel.
String[] buttonLabels = {"Start", "Stop", "Restart"};
for (int i=0; i
*
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class TJProgressBar extends JApplet {
Container container = null;
JButton startButton, stopButton;
JTextField inputTextField, outputTextField;
JProgressBar pBar = 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(3,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 and add a progress bar to the remaining
// display area.
pBar = new JProgressBar();
pBar.setStringPainted(true);
Border border = BorderFactory.createLineBorder(Color.red, 2);
pBar.setBorder(border);
pBar.setBackground(Color.white);
pBar.setForeground(Color.blue);
pBar.setMinimum(0);
pBar.setMaximum(Integer.parseInt(inputTextField.getText()));
container.add(pBar);
// 7. Create a timer object.
timer = new Timer(0, new TimerListener());
}
// 8. 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;
pBar.setValue(counter);
outputTextField.setText(Integer.toString(sum));
}
else {
outputTextField.setText("0");
}
if (counter >= Integer.parseInt(inputTextField.getText()))
timer.stop();
}
}
// 9. 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") {
outputTextField.setText("");
if (inputTextField.getText() != " ") {
pBar.setMaximum(Integer.parseInt(
inputTextField.getText()));
sum = 0;
counter = 0;
timer.start();
}
}
else if (button.getText() == "Stop") {
timer.stop();
outputTextField.setText("");
sum = 0;
counter = 0;
pBar.setValue(0);
}
}
}
}
Listing 17.3 ProgressMonitor (TProgressMonitor.java)
/*
*
*/
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class TProgressMonitor 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(5000);
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;
}
}
}
}
Listing 17.4 ProgressMonitorInputStream (TProgressMonitorIS.java)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class TProgressMonitorIS {
static ProgressMonitorInputStream progressInput = null;
static JFrame frame = null;
static WindowEventHandler handler = null;
public static void main(String[] args) {
// 1. Create a frame and get its content pane.
frame = new JFrame ("Parent Frame");
Container container = frame.getContentPane();
frame.setVisible(true);
frame.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
frame.setSize(350, 200);
frame.show();
// 2. Event handler for closing the window.
handler = new WindowEventHandler();
frame.addWindowListener(handler);
// 3. Give the correct usage at the command line.
// in case it is needed
if (args.length != 1) {
System.out.println("Legal Usage: " +
"java TProgressMonitorIS ");
System.exit(-1);
}
try {
// 4. Create a file and file input stream objects.
File file = new File(args[0]);
FileInputStream inputStream = new FileInputStream(file);
// 5. Create the progress monitor for the input stream.
progressInput = new ProgressMonitorInputStream(
container,
"Reading File: " + args[0],
inputStream);
// 6. Get a handle on the progress monitor of the
// progressInput object, and assign the time periods
// to decide to pop up and to pop up the dialog box.
ProgressMonitor monitor =
progressInput.getProgressMonitor();
monitor.setMillisToDecideToPopup(0);
monitor.setMillisToPopup(0);
// 7. Read the bytes and write on the screen.
int curByte;
while((curByte = progressInput.read()) != -1) {
System.out.print((char) curByte);
try {
// 8. Make this thread sleep, if the process
// is too quick to notice the progress dialog box.
Thread.sleep(0); // Set this to some value,
// say 100, on faster machines
} catch (InterruptedException ie) {
System.out.println(
"Thread Interruption Occurred!");
}
// 9. Close the progress input stream filter.
if(monitor.isCanceled()) {
progressInput.close();
}
}
10. //Close the progressInputstream.
progressInput.close();
} catch (Exception e) {
System.out.println("....File Reading Interrupted....");
}
}
}
// 11. Create the listener class for window closing.
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
System.exit(-1);
}
}