user542719
user542719

Reputation: 307

how to lose the focus of JtextFeild onClick

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

Upvotes: 0

Views: 1286

Answers (1)

brian_d
brian_d

Reputation: 11395

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

Upvotes: 2

Related Questions