tags:

views:

5514

answers:

2

Hello,

I am working with Java, and I am trying to make a cell in JTable to be editable. My class implements TableModel and extends AbstractTableModel (so that I can use the method fireTableCellUpdated(rowIndex, columnIndex)), and I have implemented the methods isCellEditable() and setValueAt(). I represent a single cell in the table as an object of class Cell.

Now here is my problem: the cell is already editable, and when I click on it, the cursor appears in the cell, however, there appears also a string in the cell like this: Cell@1e63e3d. I delete this string and put in the cell the value, which I want to put, then click Enter and it works fine. But I want when I click on the cell there to appear nothing, an empty string, and not Cell@1e63e3d. And I don't know how to set this empty string as default and where.

My Cell class stores information (characteristics) about the cell like color of the cell, and its value as instance variables.

Thank you in advance, and please, tell me if you need more information.

+1  A: 

Are you using an appropriate TableCellEditor to display the component for editing?

class MyTableCellEditor
        extends DefaultCellEditor
{
    @Override
    public Component getTableCellEditorComponent(
            JTable table,
            Object value,
            boolean isSelected,
            int row,
            int column)
    {
        final JTextField c = (JTextField) super.getTableCellEditorComponent(
            table,
            ((Cell) value).text, // edit the text field of Cell
            isSelected,
            row,
            column);

        c.selectAll(); // automatically select the whole string in the cell
        return c;
    }
}

You will need to tell your table to use this custom cell editor, in addition to the custom cell renderer.

myTable.setDefaultEditor(Cell.class, new MyTableCellEditor());
Zach Scrivena
+4  A: 

Have you set a TableCellRenderer and TableCellEditor for your JTable?

For displaying a cell, the TableCellRenderer is used to render the contents for a location from the TableModel. By default, it will use the toString method of the Object in that location, so that would explain the Cell@1e63e3d being displayed in the cell -- that is the result of the toString method being called on your Cell object.

By writing a custom cell renderer (a class that implements TableCellRenderer), you'll be able to return a Component you want to use to display the Cell object, using the getTableCellRendererComponent method. In your case, you may want to subclass a JLabel which implements TableCellRenderer and will set the contents of the label to reflect the contents of your Cell object.

As for editing a cell, the TableCellEditor receives the Object from the TableModel when you want to edit a cell with in the JTable. The TableCellEditor will return a Component which is used to edit the cell contents (the Object) using the getTableCellEditorComponent method.

In the case you provide, I think that making a JTextField which implements the TableCellEditor interface will be able to do the job for you. When you override the getTableCellEditorComponent, check that you have an instance of the Cell object (i.e. object instanceof Cell) and if that's the case, initialize your JTextField to contain the contents of your Cell object that you want to display or edit.

Recommended reading: I found the Rendering cells in Swing's JTable component article from IBM developerWorks to be quite helpful in learning how to deal with JTables and their cell rendering and editing features. In particular, the Creating custom renderers and Editiing table cells sections may be of interest.

coobird