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.
Andrew Thompson - 01 Nov 2006 04:16 GMT
...
> 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.