• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox and focusGained  RSS feed

 
Gary Martinkus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JComboBox and I need to know when it gets focus on it. I am using the below code, but nothing is happening when I move into the JComboBox field. It does not go into the setFieldComment method.
....
private javax.swing.JComboBox modifyAccessField;
....
modifyAccessField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent event) {
setFieldComment(event);
}
});
....
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this might be a stupid question, but: with "move into the field" you don't mean to move the mouse cursor over the combo box? in this case you would have to listen to a MouseEvent (mouseEntered or similar).
in your sample code you have no System.out.print - have you done any console prints to be sure that the event is really not fired?
chantal
 
Gary Martinkus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chantal, I do me tab into the field or click in the field, not mouse over it. I have display statements in the setFieldComment method and it doesn't get displayed.
...
private void setFieldComment(java.awt.event.FocusEvent event) {
// Set the component that has the focus.
java.awt.Component currComponent = event.getComponent();
System.out.println("IN SET COMMENT");

// Set the appropriate field message.
if (currComponent == userIdField) {
// Set the user ID field comment.
fieldCommentLabel.setText("Enter the user�s login ID.");
}
...
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hm, your code seems fine to me.
I checked the bugparade and there is an entry concering focusGained, though it is quite a special case.
http://developer.java.sun.com/developer/bugParade/bugs/4707457.html
What you could do is:
- run your program with different java versions.
- write a little test program (just a little frame with two components where you can test the focusGained/focusLost method) - don't copy the source code of your program, rather write a very simple, and different test implementation.
If you encounter any problems, run it with different java versions. If the program behaves differently depending on the version and you find the behaviour with the latest version buggy, than post a bug in the bug parade.
- put more System.out.println in your code, especially in front of, and after the addFocusListener call, and directly at the start of focusGained (before setFieldComment()).
Chantal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!