开发者

how to lose the focus of JtextFeild onClick

开发者 https://www.devze.com 2023-02-05 13:49 出处:网络
how to lose focus of JFormatedTextfeild when click on Calender Window private void dateTxtFocusGained(java.awt.event.FocusEvent evt) {

how to lose focus of JFormatedTextfeild when click on Calender Window

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


One hack I have used in the past is to toggle the focusability of the component. In your onClick event:

//we just want to lose current caret focus
//but still have textfield be focusable
dateTxt.setFocusable(false);
dateTxt.setFocusable(true);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号