FileDocCategorySizeDatePackage
TextEventDemo.javaAPI DocExample4046Tue Dec 12 18:58:52 GMT 2000None

TextEventDemo

public class TextEventDemo extends JApplet implements ActionListener

Fields Summary
JTextField
textField
JTextArea
textArea
JTextArea
displayArea
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)
Handle button click.

        displayArea.setText("");
        textField.requestFocus();
    
public voidinit()

        JButton button = new JButton("Clear");
        button.addActionListener(this);

        textField = new JTextField(20);
        textField.addActionListener(new MyTextActionListener());
        textField.getDocument().addDocumentListener(
            new MyDocumentListener("Text Field"));

        textArea = new JTextArea();
        textArea.getDocument().addDocumentListener(
            new MyDocumentListener("Text Area"));
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(200, 75));

        displayArea = new JTextArea();
        displayArea.setEditable(false);
        JScrollPane displayScrollPane = new JScrollPane(displayArea);
        displayScrollPane.setPreferredSize(new Dimension(200, 75));

        JPanel contentPane = new JPanel();
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        contentPane.setLayout(gridbag);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;

        /*
         * Hack to get around gridbag's refusal to allow
         * multi-row components in anything but the left column.
         */
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BorderLayout());
        leftPanel.add(textField, BorderLayout.NORTH);
        leftPanel.add(scrollPane, BorderLayout.CENTER);

        c.gridheight = 2;
        gridbag.setConstraints(leftPanel, c);
        contentPane.add(leftPanel);

        c.weighty = 1.0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridheight = 1;
        gridbag.setConstraints(displayScrollPane, c);
        contentPane.add(displayScrollPane);

        c.weighty = 0.0;
        gridbag.setConstraints(button, c);
        contentPane.add(button);

        textField.requestFocus();

        setContentPane(contentPane);