如何知道JCalendar是否为空?

时间:2014-03-24 16:43:45

标签: java swing select jcalendar

我有一个简单的Java程序,带有 JCalendar 。 我需要知道我的JCalendar calendario是否为空,这意味着用户是否选择了日期。我在考虑像calendario.isEmpty这样的方法,但它不存在。也许我可以将calendarionull进行比较,但它不起作用。 我正在使用com.toedter.calendar.JDateChooser

2 个答案:

答案 0 :(得分:4)

如果您使用com.toedter.calendar.JDateChooser,最好的方法是检查调用实例方法getDate()的返回日期。如果返回的日期为null,则表示用户尚未选择日期。 e.g:

public class TestCalendar extends JFrame implements ActionListener {

  private JDateChooser dateChooser;

  public TestCalendar() {
    super("Simple");
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    dateChooser = new JDateChooser();
    add(dateChooser);
    JButton button = new JButton("Check");
    button.addActionListener(this);
    add(button);
    setSize(300, 100);
  }

  public void actionPerformed(ActionEvent e) {
    Date date = dateChooser.getDate();
    if (date == null) {
      JOptionPane.showMessageDialog(TestCalendar.this, "Date is required.");
    }
  }

  public static void main(String[] args) {
    new TestCalendar().setVisible(true);
  }

}

答案 1 :(得分:1)

你也可以试试这个

if(jDateChooser1.getDate()==null){
   JOptionPane.showMessageDialog(this, "Date not selected");
}
相关问题