0

单击日历窗口时如何失去 JFormatedTextfeild 的焦点

private void dateTxtFocusGained(java.awt.event.FocusEvent evt) {
    // TODO add your handling code here:
    PickDate ex=new PickDate();       
    // dateTxt.setText(Helper.pickDate.toString());
}

...

private class MyDateListener implements DateListener
{
    public void dateChanged(DateEvent e)
    {
        Calendar c = e.getSelectedDate();
        if (c != null) {        
            formatTxt.setText(c.getTime()); 
            PickDate.this.dispose(); // pickdate is nothing but JFrame in which calender shows 
        }
        else {
            System.out.println("No time selected.");
        }
    }
}
4

1 回答 1

2

我过去使用的一种技巧是切换组件的可聚焦性。在您的 onClick 事件中:

//we just want to lose current caret focus
//but still have textfield be focusable
dateTxt.setFocusable(false);
dateTxt.setFocusable(true);
于 2011-01-18T18:43:59.190 回答