简体   繁体   中英

Toedter Get Date From JDateChooser

I am using toedter JDateChooser, and I am having problems retrieving the date picked from it.

jDateChooser2.setDateFormatString("dd-MMMM-yy");
jDateChooser2.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        jDateChooser2MouseClicked(evt);
    }
});

private void jDateChooser2MouseClicked(java.awt.event.MouseEvent evt) {                                           
    Date dateFromDateChooser = jDateChooser2.getDate();
    System.out.println(dateFromDateChooser);
}    

How can I retrieve the date? Is there a better way to do it? I think the listener is not being fired or triggered. i tried replacing the listener with:

    System.out.println("triggered");

Still there are no output.

Basically, you don't want to listener for MouseEvent s, as these could be changing the state of the component in a number of ways, most of which you don't want to know about.

You should be monitoring the date property change event, for example...

JDateChooser dateChooser = new JDateChooser();
dateChooser.addPropertyChangeListener("date", new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        Date date = (Date)evt.getNewValue();
        System.out.println("Date changed " + date);
    }
});

Just beware, this could be triggered in response to calling setDate or by the user selecting a date from the picker, generally, you won't be able to tell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM