0

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 Answers1

0

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 
    }
});
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
  • Btw , is there anyway we could check if user input new letter in textarea. it will return true . For an example, if user type each word, it will return true , else it will return false as i am creating a counter. –  Feb 15 '19 at 08:52
  • I got it , will just need to include in given insertUpdate and removeUpdate. Thanks. –  Feb 15 '19 at 09:03