Prevent FocusEvents when Component is not focused
I have the following sample-code:
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class SampleFocus extends JFrame {
public SampleFocus(String titel) {
setTitle(titel);
JTextField txtField1 = new JTextField("default-click");
JTextField txtField2 = new JTextField("alternative-Text");
JTextField txtField3 = new JTextField("own diaolog textfield");
JTextArea dummyLabel = new JTextArea(10, 20);
dummyLabel.setText("empty textarea, which is focusable");
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
add(txtField1);
add(dummyLabel);
add(txtField2);
JDialog altDialog = new JDialog(this);
altDialog.add(txtField3);
altDialog.setVisible(true);
altDialog.pack();
FocusAdapter myFocusListner = new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (e.getComponent() instanceof JTextField) {
System.out.println("gained for TextField: "
+ ((JTextField) e.getComponent()).getText());
} else {
System.out.println("gained for component: " + e.getComponent());
}
}
};
txtField1.addFocusListener(myFocusListner);
txtField2.addFocusListener(myFocusListner);
txtField3.addFocusListener(myFocusListner);
// dummyLabel.addFocusListener(myFocusListner);
}
public static void main(String[] args) {
JFrame frame = new SampleFocus("FocusListener - sample");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
When I switch from the Frame to the dialog I get the proper event. When I switch back to the Frame I get as well the proper FocusEvents. My problem is, that when I switch back, I get as well the FocusEvents for components 开发者_如何学Pythonwhich I am not interested in.
e.g.
Select 'default-click' ==> Dialog/Textfield ==> Frame/'empty textarea'
Result: I get a FocusGained-Event for 'default-click' although the component has NOT the focus.
Desired Result: Either
Component 'default-click' does not get the FocusEvent OR
distinguish if the component really received the Event properly (e.g. I could have clicked into it as well)
Workaround I found:
Attach to the JTextArea as well a FocusListener. Problem is, that this would mean, I need to attach to ALLLL of my components a Listener. Which is hardly possible. Any ideas?
Any ideas how to get the result?
Thx LeO
Its working as intended. You select "default-click". It gains focus. You select Dialog. Frame and "default-click" loses focus. You select Text area. Now, things becomes interesting. Dialog loses focus. Frame gets focus. "default-click" also gains focus, because he had it when Frame was active. And then "default-click" loses focus, textarea gains focus (because you clicked on text area).
Try combine focusGained and focusLost events.
Or tell me more what are you trying to accomplish, maybe I can help.
The way I found the workaround was to check in the focusGained-method if the current component is in an own JDialog and if previous component was a JTextField in a JFrame. If so, then I memorize the gained focused component, change the focus to another component and when I receive a focusLost I request it back to the previously memorized component.
Some kind of hack, but it works.... If there are any better ideas, I am interested in ...
精彩评论