我正在为Javaapp使用JDateChooser(这是我第一次使用它)。我想在JDateChooser为空时捕获错误,代码如下:
if(dcs1.getDate().toString().isEmpty()){
lblOk.setText("Empty");
}else{
Date d = dcs1.getDate();
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
String t = df.format(d);
lblOk.setText(t);
}
JLabel lblOk;
JDateChooser dcs1;
但它不起作用。任何人都可以帮我。
Date date;
date = JDateChooser.getDate();
if (date == null)
{
JOptionPane.showMessageDialog(null, "Choose Date from Right Box.", "Error", JOptionPane.ERROR_MESSAGE);
JDateChooser.grabFocus();
return false;
}
String s = ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText();
if (s.equals("") {
JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
if (jDateChooserBirthDate.getDate() == null) {
JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
if(jDateChooser1.getDate() == null){
JOptionPane.showMessageDialog(this, "Date not selected","No selection",JOptionPane.INFORMATION_MESSAGE);
jDateChooser1.requestFocusInWindow();
}
我使用了DateChooserCombo。下面的代码对我有效。
String test = dateChooserID.getText();
if(!test.equals("")){
//DO WHATEVER YOU WANT (DATE IS SELECTED IN THIS CASE);
}
else{
JOptionPane.showMessageDialog(this, "date not choosen");
}