Reputation: 320
I use JDateChooser
and some combobox in one frame. When i open popupcalendar in JDateChooser and do mouse click outside, this popupmenu close.
Problem: When i open this calendar and then click to any combobox, calendar popupmenu does not close. Why does it heppend and how i can close or hide it in code.
I have try it like this
popup.setVisible(false)
,
but it does not work. if i try like
popup.hide()
popupmenu will never close.
Upvotes: 0
Views: 1415
Reputation: 131
I was having the same issue as the OP, however the accepted answer didn't really help me out. I found a solution though, so I thought I'd post it here.
Looking through the source code for JDateChooser (1.4), I came across this in the constructor:
popup = new JPopupMenu() {
private static final long serialVersionUID = -6078272560337577761L;
public void setVisible(boolean b) {
Boolean isCanceled = (Boolean) getClientProperty("JPopupMenu.firePopupMenuCanceled");
if (b
|| (!b && dateSelected)
|| ((isCanceled != null) && !b && isCanceled.booleanValue())) {
super.setVisible(b);
}
}
};
popup.setLightWeightPopupEnabled(true);
popup.add(jcalendar);
Notice how the "setVisible" method for the popup is being overridden with custom functionality. There is something about this that doesn't seem to play nice with combo boxes.
To fix this, I used my own class, extending JDateChooser, and added this to my constructor:
this.popup = new JPopupMenu();
this.popup.setLightWeightPopupEnabled(true);
this.popup.add(this.jcalendar);
Basically we are redefining the popup to NOT override the setVisible functionality. The popup now hides properly when I click on combo boxes.
EDIT After further testing, I found that I could no longer select a month from the combo box in the date chooser without it closing (big problem). See below for the full code from my revised custom date chooser class:
public class CustomDateChooser extends JDateChooser {
public CustomDateChooser() {
super();
this.popup = new JPopupMenu() {
@Override
public void setVisible(final boolean b) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
handleVisibility(b);
}
});
}
private void handleVisibility(boolean b) {
if (!jcalendar.getMonthChooser().getComboBox().hasFocus()) {
super.setVisible(b);
}
}
};
this.popup.setLightWeightPopupEnabled(true);
this.popup.add(this.jcalendar);
}
}
By overriding JPopupMenu's setVisible() method, we are now only calling setVisible when the month chooser combo box does not have focus. Note that we must use threading (invokeLater) to make this work, otherwise the code would execute before the combo box actually obtains focus.
Upvotes: 3
Reputation: 145
try isShowingPopup = false;
private void PopMenuFocusLost(java.awt.event.FocusEvent evt) {
isShowingPopup = false;
}
Upvotes: -1