Question

I have two JDateChoosers, One with label 'Start Date' & another with 'End Date'.I have two radio buttons 'single Day' & 'Multiple Day'.If I select 'Single Day' I want to display same date to 'End Date' which selected in 'Start Date'. And I also want to clear these JDateChooser fileds on CLEAR_BUTTON_CLICK.How do I write this? I am using this control first time..

Plz,help me..

Thanks in advance..

Was it helpful?

Solution

I'm assuming that you are talking about JDateChooser from JCalendar. Am I correct? The JDateChooser fires a PropertyChangeEvent when its date is changed. So, to set the date of another JDateChooser, you need to add an event handler to the "source" component to deal with the change event. When it is fired, you take the date of the component using getDate() method and set it to the target component, using setDate() method. As you are using a component suite that I don't have installed here, it is difficult to implement a correct solution to you.

Take a look in the docs: http://www.toedter.com/en/jcalendar/api/com/toedter/calendar/JDateChooser.html

I think that reading this you will be able to do what you want.

Edit: Here is some code. Try to use it. I really don't have certain that it will works since I didn't test it.

// sourceDateChooser and targetDateChooser MUST be final, 
// since they will be accessed inside a anonymous inner class

sourceDateChooser.addPropertyChangeListener( new PropertyChangeListener(){
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        // the docs of JDateChooser says that when the date is modified, a "date" property change is fired
        if ( evt.getPropertyName().equals( "date" ) ) {
            targetDateChooser.setDate( sourceDateChooser.getDate() );
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top