KeyListeners

J

Jason Cavett

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
 
J

Jason Cavett

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

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

hiwa

Jason said:
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
Don't use KeyListener for that kind of purpose.
Use ActionListener for JTextField.
You could also use DocumentFilter on the Document of the JTextField.
 
J

Jason Cavett

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

hiwa

Jason said:
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.
Use DocumentListener, then.
 
M

Michael Rauscher

Jason said:
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

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
 
B

Brandon McCombs

Michael said:
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 :(

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).
 
J

Jason Cavett

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top