Java 是否可以检测 JCalendar JDateChooser 字段上的日期更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4156305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to detect a date change on a JCalendar JDateChooser field?
提问by ghickman
I'd like to detect when the date is changed in a JDateChooserfield so that I can update another field.
我想检测JDateChooser字段中的日期何时更改,以便我可以更新另一个字段。
Is this possible? And if so where should I be starting? I've already looked at the documentation and unfortunately there are no methods for adding something like an ActionListener or StateChangeListener (my first thoughts).
这可能吗?如果是这样,我应该从哪里开始?我已经看过文档,不幸的是没有方法可以添加类似 ActionListener 或 StateChangeListener 的东西(我的第一个想法)。
采纳答案by Catalina Island
com.toedter.calendar.JCalendar
inherits a listenerList
from JComponent
and it implements java.beans.PropertyChangeListener
. I'd add a listener and see what comes though.
com.toedter.calendar.JCalendar
继承listenerList
fromJComponent
并实现java.beans.PropertyChangeListener
. 我会添加一个监听器,看看会发生什么。
Edit: I think you can use addPropertyChangeListener()
the same way JCalendar
does.
编辑:我认为您可以使用addPropertyChangeListener()
相同的方式JCalendar
。
JDateChooser chooser = new JDateChooser();
chooser.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("date".equals(e.getPropertyName())) {
System.out.println(e.getPropertyName()
+ ": " + (Date) e.getNewValue());
}
}
});
this.add(chooser);
回答by trashgod
I've not tried it, but addDateListener(DateListener listener)
looks appropriate.
我没试过,但addDateListener(DateListener listener)
看起来很合适。