Search code examples
javaswingjtabletablecellrenderertablecelleditor

Cell in a JTable not editable when cell type is not String?


I have my own TableModel implementation designed to show data from a SQL database. I have overidden all the necessary methods, using a String array for column names, an arraylist<Object[]> for data and Class<?>[] array for all the different types that can be retrieved from the database. I also have a boolean array that dictates which columns are editable and which are not. Before I had everything in the table stored as an Object and hadn't implemented the types part yet and it was working well. Now that I've added the types to the model, I can't edit any column of the int type even though that column is editable in my boolean array. I have overridden the isEditable() method to simply return the value from that boolean array, and that returns true on the into column in question - but it is still uneditable. Is this define behaviour or is something wrong? I'm afraid I can't post code at the moment because I'm on my phone, my laptop has no internet connection at the moment and won't until the end of the week. I've searched but Google only shows lots of questions about making cells editable or uneditable, not why you cannot edit an int column. EDIT: here's a pastebin showing my problem: http://pastebin.com/cYJnyyqy

Using jdk7 and only the string column is editable, even though isEditable() returns true for all columns.


Solution

  • Hmm. I've never used the raw types (e.g. int.class) for the getColumnClass(). I've always used the "wrapped" types, e.g. Integer.class.

    Try changing your Class<?>[] types to use the wrapped classes instead of the primitives. e.g.

     Class<?>[] types = {
                String.class,
                Character.class,
                Integer.class,
                ...
    

    This may be needed for Swing to find the correct Renderer/TableCellEditor. But I'm not sure...