Hello,
I am trying to write a text field so that when the text in the
JTextField is updated, the title bar of the main window updates with
it. However, I am having a problem. I register my listener below:
projectNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
frame.setTitle(projectNameTextField.getText());
}
});
The problem is that the title is always one character behind what I
have typed. Like this...
Project Name: Project
Title Bar: Project
Project Name: Projec
Title Bar: Project
Project Name: Proje
Title Bar: Projec
I tried other listeners (such as the CaretListener) but that updates
too soon (do to something else I'm doing - I don't want the title bar
to update just because I clicked in the title bar box). From what I
can tell the problem that I'm having is that I'm getting the text in
the JTextField before the event has finished firing which is why the
text hasn't been updated at that point. Is there a better approach to
what I am doing?
Thanks
Jason Cavett - 13 Nov 2006 20:13 GMT
> Hello,
>
[quoted text clipped - 28 lines]
>
> Thanks
Heh, well, about two minutes after making this post, I remembered there
are different types of key listeners. I needed to use the keyReleased
method rather than keyPressed.
Thanks.
hiwa - 13 Nov 2006 23:54 GMT
> Hello,
>
[quoted text clipped - 29 lines]
>
> Thanks
Don't use KeyListener for that kind of purpose.
Use ActionListener for JTextField.
You could also use DocumentFilter on the Document of the JTextField.
Jason Cavett - 14 Nov 2006 03:45 GMT
> > Hello,
>
[quoted text clipped - 31 lines]
> Use ActionListener for JTextField.
> You could also use DocumentFilter on the Document of the JTextField.
Well, the ActionListener doesn't work the way I want it to - the user
has to hit the "Enter" key. I want things to update as the person is
typing. This is why I ended up going with the KeyListener.
hiwa - 14 Nov 2006 04:18 GMT
> > > Hello,
> >
[quoted text clipped - 35 lines]
> has to hit the "Enter" key. I want things to update as the person is
> typing. This is why I ended up going with the KeyListener.
Use DocumentListener, then.
Michael Rauscher - 14 Nov 2006 04:20 GMT
Jason Cavett schrieb:
> Hello,
>
[quoted text clipped - 9 lines]
>
> The problem is that the title is always one character behind what I
The real problem is that you don't use the right listener.
If you rely on KeyListener e. g. JTextField.setText("XYZ") wouldn't
result in an updated window title :(
What you want is to update the window's title based on the current
content of the JTextField's model. Since JTextField uses Document as
model, you have to deal with the Document. There are several ways:
provide an own implementation of Document (don't do it - there's
absolutely no need to), use a DocumentFilter or a DocumentListener.
Bye
Michael
Brandon McCombs - 14 Nov 2006 05:57 GMT
> Jason Cavett schrieb:
>> Hello,
[quoted text clipped - 15 lines]
> If you rely on KeyListener e. g. JTextField.setText("XYZ") wouldn't
> result in an updated window title :(
In addition to that, a KeyListener also won't catch a paste operation
into the textfield since a key isn't actually pressed that had its
corresponding character appear (especially if you use the mouse to do
the paste operation).
> What you want is to update the window's title based on the current
> content of the JTextField's model. Since JTextField uses Document as
[quoted text clipped - 4 lines]
> Bye
> Michael
Jason Cavett - 14 Nov 2006 16:53 GMT
> > Jason Cavett schrieb:
> >> Hello,
[quoted text clipped - 27 lines]
> > Bye
> > Michael- Hide quoted text -- Show quoted text -- Hide quoted text -- Show quoted text -
Okay, that explains it a bit better. I made the change suggested (used
a DocumentListener instead of a KeyListener) and copy and paste does
work via the mouse (I hadn't even considered paste not working -
something I take for granted usually).
Thanks again.