Updating JTable Cell with Date type

i've followed some stackoverflow responses to this problem and every answer i found contributed a bit to solve the problem, however i'm still stuck.

I still can't update a table cell with the Date Type, i click the cell and edit it, but when i press Enter it won't update, it won't even let me leave the cell it just displays a red border around it. I would appreciate some insight, thank you.

getColumnClass method:

@Override
public Class getColumnClass(int column) throws IllegalStateException {
    String columnName;

    if (!connectedToDatabase) {
        throw new IllegalStateException("Not Connected to Database");
    }

    columnName = getColumnName(column);

    if(columnName.equals("id")) {
        return java.lang.Integer.class;
    }

    switch (table) {
        case 0:
            if (columnName.equals("dn") || columnName.equals("ade")) {
                return java.util.Date.class;                    
            } else {
               return java.lang.String.class;
            }
        default:
            try {
                String className = metaData.getColumnClassName(column + 1);
                return Class.forName(className);
            } catch (SQLException | ClassNotFoundException ex) {
                return Object.class;
            }
    }
}

getValueAt method:

@Override
public Object getValueAt(int row, int column) throws IllegalStateException {
    if (!connectedToDatabase) {
        throw new IllegalStateException("Not Connected to Database");
    }

    try {
        resultSet.absolute(row + 1);
        return resultSet.getObject(column + 1);
    } catch (SQLException ex) {
        return "";
    }
}

setValueAt method:

@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
    String columnName = getColumnName(columnIndex);
    java.sql.Date sqlDate;
    switch (table) {
        case 0:
            if (columnName.equals("dn") || columnName.equals("de")) {
                sqlDate = new java.sql.Date(((java.util.Date) value).getTime());
                try {
                    resultSet.absolute(rowIndex + 1);
                    resultSet.updateDate(columnIndex + 1, sqlDate);
                    resultSet.updateRow();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }

            } else {
                try {
                    resultSet.absolute(rowIndex + 1);
                    resultSet.updateString(columnIndex + 1, (String) value);
                    resultSet.updateRow();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
    }
    this.fireTableCellUpdated(rowIndex, columnIndex);
}

Answers


The red border around the cell indicates that the date format you entering is wrong. If you are using a default date editor then try entering a date with a simple format, ie: DD/MM/YYYY.

For example here is a red border for a DD-MM-YYYY format that cannot be parsed by Date:

A default editor that is used by JTable for date columns attempts to create Date object from the entered value. Either comply with the expected format or provide you own custom editor. See How to Use Tables for some examples.


Need Your Help

Override attribute's methods in scala

scala oop composition accessor mutators

In scala it's quite easy to override the default accessors and mutators; this makes me feel much more comfortable with public attributes because it allows me to modify access behavior at a later time

How do I write statusbar in JTextPane?

java swing java-ee statusbar jtextpane

When I click in JTextPane html content, that html element name should be in statusbar. How can I write stausbar in JTextPane? If you have any sample code provide me.