简体   繁体   English

Java:如何将一个JDateChooser的值设置为另一个JDateChooser?

[英]Java: How to set value of one JDateChooser to another JDateChooser?

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'. 我有两个JDateChoosers,一个带有“开始日期”标签,另一个带有“结束日期”标签。我有两个单选按钮“单日”和“多日”。如果选择“单日”,我想在“在“开始日期”中选择的“结束日期”。 And I also want to clear these JDateChooser fileds on CLEAR_BUTTON_CLICK.How do I write this? 而且我还想清除CLEAR_BUTTON_CLICK上的这些JDateChooser文件。我该怎么写? I am using this control first time.. 我第一次使用此控件。

Plz,help me.. 请帮帮我..

Thanks in advance.. 提前致谢..

I'm assuming that you are talking about JDateChooser from JCalendar. 我假设你是从JCalendar谈论JDateChooser。 Am I correct? 我对么? The JDateChooser fires a PropertyChangeEvent when its date is changed. 更改日期后,JDateChooser将触发PropertyChangeEvent。 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. 因此,要设置另一个JDateChooser的日期,您需要在“源”组件中添加一个事件处理程序以处理change事件。 When it is fired, you take the date of the component using getDate() method and set it to the target component, using setDate() method. 触发后,可以使用getDate()方法获取组件的日期,并使用setDate()方法将其设置为目标组件。 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 看一下文档: 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() );
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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