FileDocCategorySizeDatePackage
URLMonitorPanel.javaAPI DocExample2521Mon Mar 15 00:49:04 GMT 2004javathreads.examples.ch11.example1

URLMonitorPanel

public class URLMonitorPanel extends JPanel implements URLPingTask.URLUpdate

Fields Summary
Timer
timer
URL
url
URLPingTask
task
JPanel
status
JButton
startButton
JButton
stopButton
Constructors Summary
public URLMonitorPanel(String url, Timer t)

	setLayout(new BorderLayout());
        timer = t;
        this.url = new URL(url);
        add(new JLabel(url), BorderLayout.CENTER);
	JPanel temp = new JPanel();
        status = new JPanel();
        status.setSize(20, 20);
        temp.add(status);
        startButton = new JButton("Start");
        startButton.setEnabled(false);
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                makeTask();
                startButton.setEnabled(false);
                stopButton.setEnabled(true);
            }
        });
        stopButton = new JButton("Stop");
        stopButton.setEnabled(true);
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                task.cancel();
                startButton.setEnabled(true);
                stopButton.setEnabled(false);
            }
        });
        temp.add(startButton);
        temp.add(stopButton);
	add(temp, BorderLayout.EAST);
        makeTask();
    
Methods Summary
public voidisAlive(boolean b)

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                status.setBackground(b ? Color.GREEN : Color.RED);
                status.repaint();
            }
        });
    
public static voidmain(java.lang.String[] args)

        JFrame frame = new JFrame("URL Monitor");
        Container c = frame.getContentPane();
	c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
        Timer t = new Timer();
        for (int i = 0; i < args.length; i++) {
            c.add(new URLMonitorPanel(args[i], t));
        }
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.show();
    
private voidmakeTask()

        task = new URLPingTask(url, this);
        timer.schedule(task, 0L, 5000L);