开发者

Java action performed when text is entered into a textarea

开发者 https://www.devze.com 2022-12-28 18:59 出处:网络
I\'m wondering if someone can help me out. I entered a character into a text area from a button, and want to use the string entered into the textarea t开发者_JAVA技巧o retrieve words from a list. Bear

I'm wondering if someone can help me out. I entered a character into a text area from a button, and want to use the string entered into the textarea t开发者_JAVA技巧o retrieve words from a list. Bear in mind, there could be numerous characters entered. Is it possible for a text area to detect when text has been entered and to action it?


You can add a DocumentListener to your JTextArea;

class YourClass {
   ...
   public void attachTextAreaToPanel(JPanel panel) {
      JTextArea textArea = new JTextArea();
      textArea.getDocument().addDocumentListener(new MyDocumentListener());
      panel.add(textArea);
   }
}

class MyDocumentListener implements javax.swing.event.DocumentListener {
   public void changedUpdate(javax.swing.event.DocumentEvent e) {
      // text has been altered in the textarea
   }
   public void insertUpdate(javax.swing.event.DocumentEvent e) {
      // text has been added to the textarea
   }
   public void removeUpdate(javax.swing.event.DocumentEvent e) {
      // text has been removed from the textarea
   }
}

Edit, this requires that you use Swing - and not AWT.


I assume you are refering to swing JTextArea ?

look at:

http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html

There is a part that is exactly what you are looking for.


Implements the TextListener for that textarea. Then use the conditions.

Otherwise implements ActionListener to your button. Then specify the action you want, while pressing your button.

0

精彩评论

暂无评论...
验证码 换一张
取 消