Yesterday, we ran into a small issue with JComboBox. It seems that actionEvents are fired both when you scroll over an item with the keyboard and when you press enter on it. The normal OS behaviour is to fire only when an item is actually selected, either by clicking on it with the mouse or by pressing enter on the item.
Because we're opening a new window based on the selection, it doesn't relly work very well when the action is fired just because the item gets focus. After messing around with addActionListener, addItemListener, and addPopupMenuListener,
we concluded that there's relly no support for getting only an event when an item is selected.
Adding a PopupMenuListener gets very close, but if you open a new window inside that event, a NullPointerException will occur later because of some issues with MenuManager being a singleton.
In the end, we made a hack:
-
boolean cancelled;
-
-
cancelled = true;
-
}
-
if (!cancelled) {
-
public void run() {
-
while (true) {
-
if (!isPopupVisible()) {
-
break;
-
}
-
try {
-
wait(50);
-
}
-
}
-
}
-
});
-
}
-
}
-
cancelled = false;
-
}
-
});
-
}
-
}
In other words, a listener gets an event just before the popup is closed. The listener then spawns a new thread which checks if the popup has been closed. When the popup is closed, the actionListener is notified.
This can only be classified as a hack, so if anybody has a better solution, then please tell me...



