validating a jDateChooser

2019-09-03 09:16发布

I have a jFrame in my java application which inserts the selected date into the database. I want to validate if field is empty.

The code is like this:

if(!this.jtxtDate.getSelectedItem.toString().isEmpty()) {
    idc.setDate(this.jtxtDate.getDate);
} else {
    JOptionPane.showMessageDialog(rootPane, "pls choose date");
}

3条回答
神经病院院长
2楼-- · 2019-09-03 09:34

You could just check if you can get date from jDateChooser.

if(this.jtxtDate.getDate() != null) {
    idc.setDate(this.jtxtDate.getDate);
} else {
    JOptionPane.showMessageDialog(rootPane, "pls choose date");
}
查看更多
淡お忘
3楼-- · 2019-09-03 09:41

dateChooser.setEnabled(false);

dateChooser.getCalendarButton().setEnabled(true);

So you can't edit the textfield integrated to the JDateChooser.

查看更多
虎瘦雄心在
4楼-- · 2019-09-03 09:43

Use this code inside try block:

if(
    ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText().isEmpty()
) {
    JOptionPane.showMessageDialog(this, "Date should be filled");
} else {
    //some code
}
查看更多
登录 后发表回答