user16916070
user16916070

Reputation:

How to pass JTable string date to JDateChooser?

I'm trying to pass the data from the JTable for which all the types are String then I want to pass to the JDateChooser when the user selected the row in the table. But I tried parsing the date from string to date type but still, I got an error saying illegal value.

Here is the code.

private void tableDataMouseClicked(java.awt.event.MouseEvent evt) {
    try{
        DefaultTableModel model = (DefaultTableModel) tableData.getModel();
        int selectedRowIndex = tableData.getSelectedRow(); //get selected row
        Fname.setText(model.getValueAt(selectedRowIndex,1).toString());
        Lname.setText(model.getValueAt(selectedRowIndex,2).toString());
        ageSpin.setValue(model.getValueAt(selectedRowIndex,3).toString());
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse((String)model.getValueAt(selectedRowIndex, 4).toString());
        dob.setDate(date);
        addressField.setText(model.getValueAt(selectedRowIndex,5).toString());
        phoneNumField.setText(model.getValueAt(selectedRowIndex,6).toString());
        emailField.setText(model.getValueAt(selectedRowIndex,7).toString());
    }catch(ParseException e){
        e.printStackTrace();
        Logger.getLogger(addCitizzen.class.getName()).log(Level.SEVERE, null, e);
    }
}

Here is the error I'm getting:

java.lang.IllegalArgumentException: illegal value

The program gives an error when it reaches the Date part.

Upvotes: -1

Views: 529

Answers (1)

Hima
Hima

Reputation: 1

The error is arising due to argument passed. Here is the working one:

Date date = new SimpleDateFormat("dd-MM-yyyy").parse((String)model.getValueAt(selectedRowIndex, 4).toString());

Upvotes: 0

Related Questions