0

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);
}
hbgranja
  • 55
  • 1
  • 5

1 Answers1

3

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:

enter image description here

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.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • Entering the date in the DD/MM/YYYY format worked thank you!, but i have a slight problem, the date in the table is displayed in this format: (ex: "29/Jan/1980") when i try to edit the cell it shows up as YYYY-MM-DD how can i keep the format consistent in those 2 views (DD/MM/YYYY)? – hbgranja Dec 09 '13 at 18:51
  • @user2382836 you can create custom editor. See the concept of editors and renderers in the mentioned tutorial. – tenorsax Dec 09 '13 at 20:26