0
votes

I have been wondering if it is possible to update JFrame or JDialogs based on the inputs inside jtextarea without button click. For an example, after i input some text in textarea. it should automatically update jlabel without the need of a button. I have search troughout but all the information i found is only based on button click . For an example ,

JFrame frame = new JFrame();
    frame.setLayout(new GridLayout(0, 1));
    JTextArea input = new JTextArea();
    JLabel output = new JLabel("test");

    // Condition 
    // If user input "abc" inside textfield
    // JLabel will automatically display "abc"

    frame.add(input);
    frame.add(output);
    frame.setSize(300,400);
    frame.setVisible(true);

Do i need to refresh the entire frame ? will it affect all the other textfield that user have already fill back to empty?

Thanks

1

1 Answers

1
votes

Document object contained by JTextArea receives updates.

JTextArea input = new JTextArea();
input .getDocument().addDocumentListener(new DocumentListener() {

    @Override
    public void removeUpdate(DocumentEvent e) {}

    @Override
    public void insertUpdate(DocumentEvent e) {}

    @Override
    public void changedUpdate(DocumentEvent arg0) {
            //Add logic here to check if particular word is entered.
            //if yes show label, else hide the label object 
    }
});