我正在使用JDateChooser作为Javaapp(这是我第一次使用它)。当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;
但它不起作用。 Any1可以帮助我发挥作用。
答案 0 :(得分:5)
Date date;
date = JDateChooser.getDate();
if (date == null)
{
JOptionPane.showMessageDialog(null, "Choose Date from Right Box.", "Error", JOptionPane.ERROR_MESSAGE);
JDateChooser.grabFocus();
return false;
}
答案 1 :(得分:4)
String s = ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText();
if (s.equals("") {
JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
答案 2 :(得分:1)
if (jDateChooserBirthDate.getDate() == null) {
JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
答案 3 :(得分:1)
if(jDateChooser1.getDate() == null){
JOptionPane.showMessageDialog(this, "Date not selected","No selection",JOptionPane.INFORMATION_MESSAGE);
jDateChooser1.requestFocusInWindow();
}
答案 4 :(得分:0)
我使用了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");
}