1

I'm using JDateChooser for a Javaapp (this is the 1st time i use it). I want to catch fault when the JDateChooser is empty with code like this:

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;

But it dont work. Any1 can help me plzz.

Unoken Mouny
  • 157
  • 1
  • 3
  • 14

5 Answers5

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;
        }
Irshad Khan
  • 5,670
  • 2
  • 44
  • 39
5
String s = ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText();
if (s.equals("") {
    JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
user1391111
  • 63
  • 2
  • 8
1
if (jDateChooserBirthDate.getDate() == null) {
   JOptionPane.showMessageDialog(null, "Please type birthday", "Warning!", JOptionPane.ERROR_MESSAGE);
}
Micha
  • 5,117
  • 8
  • 34
  • 47
1
    if(jDateChooser1.getDate() == null){
    JOptionPane.showMessageDialog(this, "Date not selected","No   selection",JOptionPane.INFORMATION_MESSAGE);
    jDateChooser1.requestFocusInWindow();
   }
David
  • 501
  • 1
  • 7
  • 13
0

I used DateChooserCombo. and The following code worked for me.

String test = dateChooserID.getText();
if(!test.equals("")){
//DO WHATEVER YOU WANT (DATE IS SELECTED IN THIS CASE);
}
else{
JOptionPane.showMessageDialog(this, "date not choosen");
}