使用带有RAP的SWT DateSpinner不会丢失焦点上的日历小部件

时间:2015-07-14 22:22:06

标签: java swt eclipse-rap

我正在开发一个项目,我需要在RAP应用程序网页上使用SWT DateSpinner。我将整个Datespinner API带入了我的项目,并进行了更改以在应用程序中容纳它。我试图在用户点击日历下拉列表边界之外的页面上的任何位置时处理日历小部件。

为了实现这一点,我尝试将焦点侦听器添加到窗口小部件和实际的日期微调器,但只要每个datespinner或日历复合体失去焦点,就不会执行onFocusLost()方法。我还试图添加一个鼠标事件监听器,如果鼠标按下事件发生在日历复合边界之外但我也没有执行,我尝试处理该窗口小部件。 showCalendar()方法如下所示:

private void showCalendar()
{
    if (calendarDropDown != null && !calendarDropDown.isDisposed())
    {
        calendarDropDown.dispose();
    }

    calendarDropDown = new DropDown(this);
    calendarDropDown.setUsingRelativeControlWidth(false);
    Composite composite = calendarDropDown.getDropDown();
    composite.setLayout(new FillLayout());

    composite.setData(calendarToggle);

    Calendar c = Calendar.getInstance();
    Date date = this.getDate();

    // get current date in the control
    if (date != null)
    {
        c.setTime(this.getDate());
    }

    CalendarComposite calendarWidget = new CalendarComposite(composite, c);
    calendarWidget.setNoneEnabled(this.allowNullDate);
    calendarWidget.setMinimumDate(dateToCalendar(getMinimum()));
    calendarWidget.setMaximumDate(dateToCalendar(getMaximum()));

    // background to match the calendar
    composite.setBackground(calendarWidget.getBackground());
    calendarDropDown.show(true, composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    text.addFocusListener(new FocusListener()
    {

        @Override
        public void focusLost(FocusEvent event)
        {
              dispose();
        }

        @Override
        public void focusGained(FocusEvent event)
        {
            // TODO Auto-generated method stub

        }
    });

    calendarWidget.addMainCalendarListener(new CalendarListenerAdapter()
    {
        @Override
        public void dateChanged(Calendar date)
        {
            doSetDate(date != null ? date.getTime() : null, false, true);
        }

        @Override
        public void popupClosed()
        {
            calendarDropDown.dispose();
        }
    });         
}

DropDown的show方法是

 if ((autoHide && !dropDownShell.isVisible())){            
        dropDownShell.addMouseListener(new MouseListener()
        {
            @Override
            public void mouseUp(MouseEvent e)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseDown(MouseEvent e)
            {
                if (!isInside(e.x, e.y, dropDownShell.getDisplay().getBounds()))
                {
                    dispose();
                }
            }

            @Override
            public void mouseDoubleClick(MouseEvent e)
            {
                // TODO Auto-generated method stub

            }
        });

请让我知道我可以做些什么来使CalendarWidget处理失去焦点。

1 个答案:

答案 0 :(得分:2)

我认为如果您关注的小部件位于另一个shell而不是当前关注的小部件(您的Text),则可能无法获得focusLost事件,因为每个shell都可以拥有它自己的焦点小部件。实际发生的是,Text所在的shell被取消激活,可以通知您使用ShellListener。

相关问题