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

Drawing an SVG File in WinRT

c# image webview svg windows-runtime

I'm trying to draw a SVG File. So far I've tried using an Image tag and a WebView with the svg itself or an object / embed tag. Nothing so far seems to help, and all the dll's I've found so far need

The Position of tables in hibernate

java mysql hibernate

How can we configure hibernate annotations in the Entity class so that columns are created in a specific order.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.