简体   繁体   中英

How to know if a JCalendar is empty?

I have a simple Java program with a JCalendar . I need to know if my JCalendar calendario is or not empty, it means if the user selected or not a date. I was thinking about a method like calendario.isEmpty but it doesn't exist. Maybe I can compare calendario with null but it didn't work. I am using com.toedter.calendar.JDateChooser .

If you are using com.toedter.calendar.JDateChooser , the best way is check the returned date invoking the instance method getDate() . If the returned date is null , that means that the user has not selected a date. eg:

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);
  }

}

you can try this as well

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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