why is focus lost not called

T

Timasmith

I have code that executes when focus leaves a text control.
Specifically I grab the text in the control at that time.

jTextField.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
// calls getText() and does something with it
}
});

However if I edit a text field and click on a button, swing does not
execute the focusLost - or if it does it is not with the new value.

It almost seems like a button lets me sneak out of the text control.
 
A

Andrew Thompson

Timasmith wrote:
....
It almost seems like a button lets me sneak out of the text control.

<sscce>
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Focus {
public static void main(String[] args) {
JTextField jTextField = new JTextField(20);
jTextField.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
// calls getText() and does something with it
System.out.println(e);
// ...almost seems you are not doing anything!
}
});
JTextField jTextField2 = new JTextField(20);
JPanel p = new JPanel();
p.add(jTextField);
p.add(jTextField2);
JOptionPane.showMessageDialog(null,p);
}
}
</sscce>

Andrew T.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top