0

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.

4

2 回答 2

3

我遇到了与 OP 相同的问题,但是接受的答案并没有真正帮助我。不过我找到了解决方案,所以我想我会在这里发布。

查看 JDateChooser (1.4) 的源代码,我在构造函数中遇到了这个:

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);

注意弹出窗口的“setVisible”方法是如何被自定义功能覆盖的。这似乎与组合框不太匹配。

为了解决这个问题,我使用了自己的类,扩展了 JDateChooser,并将其添加到我的构造函数中:

this.popup = new JPopupMenu();
this.popup.setLightWeightPopupEnabled(true);
this.popup.add(this.jcalendar);

基本上我们正在重新定义弹出窗口以不覆盖 setVisible 功能。当我单击组合框时,弹出窗口现在可以正确隐藏。

编辑 经过进一步测试,我发现我不能再从日期选择器的组合框中选择一个月而不关闭它(大问题)。请参阅下面的修改后的自定义日期选择器类的完整代码:

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);
}
}

通过覆盖 JPopupMenu 的 setVisible() 方法,我们现在只在月份选择器组合框没有焦点时调用 setVisible。请注意,我们必须使用线程(invokeLater)来完成这项工作,否则代码将在组合框实际获得焦点之前执行。

于 2015-09-04T16:24:06.257 回答
-1

尝试isShowingPopup = false;

 private void PopMenuFocusLost(java.awt.event.FocusEvent evt) {                                           


isShowingPopup = false;
    }   
于 2015-07-16T18:27:00.187 回答