FileDocCategorySizeDatePackage
FeedbackFrame.javaAPI DocExample2498Sat Apr 17 14:55:00 BST 2004javathreads.examples.ch07.example3

FeedbackFrame

public class FeedbackFrame extends JFrame implements Runnable

Fields Summary
private SwingTypeTester
stt
private Thread
t
private JLabel
label
private int
state
static String[]
stateMessages
Constructors Summary
public FeedbackFrame(SwingTypeTester stt)


       
        this.stt = stt;
        setupFrame();
        t = new Thread(this);
        t.start();
        pack();
        show();
    
Methods Summary
private voidcloseDown()

        stt.setupCancelled();
        hide();
        dispose();
    
private voiderror()

        t.interrupt();
        if (SwingUtilities.isEventDispatchThread())
            closeDown();
        else SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                closeDown();
           }
        });
    
public voidrun()

        // Simulate connecting to server
        for (int i = 0; i < stateMessages.length; i++) {
            setText(stateMessages[i]);
            try {
                Thread.sleep(5 * 1000);
	    } catch (InterruptedException ie) {}
	    if (Thread.currentThread().isInterrupted())
	        return;
	}
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              	stt.setupDone();
                hide();
                dispose();
	    }
        });
    
private voidsetText(java.lang.String s)

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    label.setText(s);
	        }
            });
        } catch (InterruptedException ie) {
            error();
        } catch (InvocationTargetException ite) {
            error();
        }
    
private voidsetupFrame()

        label = new JLabel();
        label.setPreferredSize(new Dimension(200, 200));
        Container c = getContentPane();
        JButton stopButton = new JButton("Stop");
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                error();
            }
        });
        c.add(label, BorderLayout.NORTH);
        c.add(stopButton, BorderLayout.SOUTH);