views:

91

answers:

3

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 to 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?

A: 

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.

dodecaplex
Thanks for this, very helpful
+1  A: 

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 extends 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.

Björn
A: 

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.

Venkats