Java action performed when text is entered into a textarea
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.
精彩评论