validating a jDateChooser

China☆狼群 提交于 2019-12-25 12:43:34

问题


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

回答1:


Use this code inside try block:

if(
    ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText().isEmpty()
) {
    JOptionPane.showMessageDialog(this, "Date should be filled");
} else {
    //some code
}



回答2:


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:


dateChooser.setEnabled(false);

dateChooser.getCalendarButton().setEnabled(true);

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



来源:https://stackoverflow.com/questions/19007738/validating-a-jdatechooser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!