JTextArea - need event rised on text changed.

I am making an application which contains a JTextArea. when user writes some data on in, some event must be invoked changing the text of the same JtextArea.

Problem is, when i change the text, Java automatically adds to JtextArea typed character, what i don't want.

program works in this way:

user presses "d" on keyboard -> on JTextArea "e" appears.

thanks for help.

[403 byte] By [DaniLa] at [2007-11-26 14:44:12]
# 1
use a TextListener
tjacobs01a at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...
# 2

TextListener is for AWT text components. In Swing you should use a DocumentListener. However, the Document is already updated at this time. So I don't think it satisfies the OP request.

It sounds like the OP wants to edit the character as it is typed, in which case you should be using a DocumentFilter. Read the JTextArea API and you will find a link to the Swing tutorial on "Using Text Components" which will explain how to use a DocumentFilter.

camickra at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...
# 3

in fact, there is no problem for me to use awt.

i have tried textlistener, it didn't help.

when i did something like this

private void TTextTextValueChanged(java.awt.event.TextEvent evt) {

TText.setText(TText.getText()+"k");

}

event rised one after another after the second line. so "k" added without stopping, not only one. is there a way to stop it?

DaniLa at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...
# 4
> in fact, there is no problem for me to use awt. Yes there is. You should never mix Swing and AWT components in the same JFrame.> is there a way to stop it? I gave you the answer. Read the tutorial.
camickra at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...
# 5

This is an example of a text area that forces upper case.

You can modify it to your needs.

private static class UpperCaseTextArea extends JTextArea {

protected Document createDefaultModel() {

return new UpperCaseDocument();

}

static class UpperCaseDocument extends PlainDocument {

public void insertString(int offs, String str, AttributeSet a)

throws BadLocationException {

if (str == null) {

return;

}

if (str.length() == 1) {

char c = str.charAt(0);

char cUpper = Character.toUpperCase(c);

super.insertString(offs, Character.toString(cUpper), a);

}

else

super.insertString(offs, str, a);

}

}

}

Rodney_McKaya at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...
# 6

> This is an example of a text area that forces upper

> case.

> You can modify it to your needs.

>

Slightly off topic, but why does your UpperCaseDocument only convert to upper case if the text to be inserted is exactly one character long? Your implementation makes it possible to paste lowercase text (if the text you are pasting is longer than one character) into your supposedly uppercase text area.

You can also use the String method toUpperCase() directly, rather than taking the Character route.

Finally, for what it's worth, the "recommended" approach (i.e. recommended by the Swing team, I think), is to use a DocumentFilter in this case, as suggested by camickr, rather than writing a custom Document. Of course, YMMV.

Torgila at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...
# 7

The UpperCaseDocument is an example from JTextField API.

You are absolutely right about the copy paste.

I was trying to modify it so it will be easier for the OP to change to his needs, but now that I look at the code again I didn't even do that.

I guess this is what happens when you try to write something quickly late at night...

class UpperCaseTextArea extends JTextArea {

protected Document createDefaultModel() {

return new UpperCaseDocument();

}

static class UpperCaseDocument extends PlainDocument {

public void insertString(int offs, String str, AttributeSet a)

throws BadLocationException {

if (str == null) {

return;

}

super.insertString(offs, str.toUpperCase(), a);

}

}

}

Rodney_McKaya at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...
# 8
thanks Rodney.this structure helps a lot. also i need to listen to whether VK_ENTER pressed.is it better way to assign an event to JTextArea or filter like str.endsWith or str.contains ?
DaniLa at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...
# 9

> is it better way

Well you where given the better way before but choose to use the option where you didn't need to write any code. So why are you looking for a better way now?

> also i need to listen to whether VK_ENTER pressed.

Most people use a KeyListener. But the "etter way" is to use KeyBindings. Of course it will require you to read the tutorial. I've already told you how to find the tutorial. So read the section on "How to Use Key Bindings".

camickra at 2007-7-8 8:31:53 > top of Java-index,Desktop,Core GUI APIs...