selecting (highlighting) the text of jdatechooser when focus gained

968 Views Asked by At

im looking for the line of code, which will select (highlight) the Date-text-string in the jDateChooser when it gets focused.

I read that I might have to do something like .selectAll();. but i cant get access to the textfield of the jDateChooser.

also jDateChooser.selectOnFocus(true); wont compile. NetBeans says: "cannot find symbol". eventhough i have imported:

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.demo.DateChooserPanel;

any ideas anyone ?

3

There are 3 best solutions below

0
On
    dateChooser.getDateEditor().getUiComponent().addFocusListener(new FocusAdapter()    {
        @Override
        public void focusGained(FocusEvent evt) {
            if (evt.getSource() instanceof JTextComponent) {
                final JTextComponent textComponent=((JTextComponent)evt.getSource());
                SwingUtilities.invokeLater(new Runnable(){
                    public void run() {
                        textComponent.selectAll();
                    }});
            }   
        }
    });
0
On

Change the library jar for calendar few libraries do not have all the symbols.

You can download from here and replace it with new one and then check:

0
On
JDateChooser dateChooser = new JDateChooser(new Date());
dateChooser.getDateEditor().getUiComponent().addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent evt) {
        ((JTextFieldDateEditor)evt.getSource()).selectAll();
    }
});