1

我将 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可以帮助我。

4

5 回答 5

6
Date date;
date = JDateChooser.getDate();

if (date == null) 
        {
            JOptionPane.showMessageDialog(null, "Choose Date from Right Box.", "Error", JOptionPane.ERROR_MESSAGE);
            JDateChooser.grabFocus();
            return false;
        }
于 2013-06-03T04:46:47.947 回答
5
String s = ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText();
if (s.equals("") {
    JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
于 2014-09-23T19:26:32.530 回答
2
if (jDateChooserBirthDate.getDate() == null) {
   JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
于 2013-12-09T08:20:45.043 回答
2
    if(jDateChooser1.getDate() == null){
    JOptionPane.showMessageDialog(this, "Date not selected","No   selection",JOptionPane.INFORMATION_MESSAGE);
    jDateChooser1.requestFocusInWindow();
   }
于 2016-11-27T23:15:56.670 回答
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");
}      
于 2018-12-10T07:27:46.727 回答