Java 在 JDateChooser 上隐藏或禁用过去的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22092365/
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
Hide or disable past dates on JDateChooser
提问by user3337385
I want to disable or hide or make the past dates in JDateChooser not selectable. How can I make this? I've tried to use .setSelectableDateRange
but it doesn't work. I also tried .setMinSelectableDate()
but still no luck. I don't know but netbeans doesn't seem to know those because those doesn't show up in code suggestions. I'm using it like this:
我想禁用或隐藏或使 JDateChooser 中的过去日期不可选择。我怎样才能做到这一点?我试过使用,.setSelectableDateRange
但它不起作用。我也尝试过,.setMinSelectableDate()
但仍然没有运气。我不知道,但 netbeans 似乎不知道这些,因为那些没有出现在代码建议中。我是这样使用它的:
public void dateset() {
jDateChooser1.getCalendar(). //What to put here? It doesn't have .setSelectableRange
}
I only tried the one that I've found on this one: How to show only date after the date of today in JCalendar
我只尝试了我在这个上找到的那个:How to show only date after today in JCalendar
I think that post was already outdated. Please help.
我认为那个帖子已经过时了。请帮忙。
采纳答案by dic19
Here:
这里:
jDateChooser1.getCalendar().
You're trying to set date's boundaries to a java.util.Calendarobject which is not possible. Maybe you're confused with getJCalendar()which returns a JCalendarobject:
您试图将日期的边界设置为java.util.Calendar对象,这是不可能的。也许您对返回JCalendar对象的getJCalendar()感到困惑:
jDateChooser1.getJCalendar().setMinSelectableDate(new Date()); // sets today as minimum selectable date
Note you can set minimum selectable date directly on date chooser:
请注意,您可以直接在日期选择器上设置最小可选日期:
jDateChooser1.setMinSelectableDate(new Date()); // sets today as minimum selectable date
Inspecting JDateChooser
source code you can see this method is just forwarded to the JCalendar
object:
检查JDateChooser
源代码你可以看到这个方法只是转发给JCalendar
对象:
public class JDateChooser extends JPanel implements ActionListener,
PropertyChangeListener {
protected IDateEditor dateEditor;
protected JCalendar jcalendar;
...
public void setMinSelectableDate(Date min) {
jcalendar.setMinSelectableDate(min);
dateEditor.setMinSelectableDate(min);
}
...
}
You may also want to take a look to How to disable or highlight the dates in java calendarfor a better understanding on IDateEvaluatorinterface which is actually the key on this whole date validation matter.
您可能还想查看如何禁用或突出显示 Java 日历中的日期,以便更好地了解IDateEvaluator接口,这实际上是整个日期验证问题的关键。
回答by Patrick
Try this example..
试试这个例子..
package chooseyourdate;
import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
private JDateChooser chooser;
public MainFrame() {
JCalendar calendar = new JCalendar(GregorianCalendar.getInstance());
chooser = new JDateChooser(calendar, new Date(), "dd.MM.yy", null);
GregorianCalendar cal = (GregorianCalendar)GregorianCalendar.getInstance();
// set the max date
cal.set(2015, 10, 10);
// MinDate is the current Date
// MaxDate you can set in the GregorianCalendar object
chooser.setSelectableDateRange(new Date(), cal.getTime());
chooser.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
// to something...
}
});
this.setSize(new Dimension(800, 600));
this.getContentPane().add(chooser, BorderLayout.NORTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}