Creating a cell in JTable for editing - the default value of a cell

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

Now here's my problem: the cell is already being edited, and when I click on it, the cursor appears in the cell, but the cell line also appears: Cell@1e63e3d. I delete this line and put the value that I want to put in the cell, then press "Enter" and it works fine. But I want when I click on a cell so that nothing appears, an empty string, not Cell@1e63e3d. And I do not know how to set this empty string by default and where.

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

Please tell me if you need more information.

+3
source share
2 answers

Have you installed TableCellRendererand TableCellEditorfor JTable?

, TableCellRenderer TableModel. toString Object , Cell@1e63e3d, , - toString, Cell.

(, TableCellRenderer), Component, Cell, getTableCellRendererComponent. JLabel, TableCellRenderer, , Cell.

, TableCellEditor Object TableModel, JTable. TableCellEditor Component, (Object) getTableCellEditorComponent.

, , , JTextField, TableCellEditor, . getTableCellEditorComponent, , Cell (.. object instanceof Cell), , JTextField, Cell, .

: Swing JTable IBM developerWorks, , JTable, . , .

+7

TableCellEditor ?

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;
    }
}

, .

myTable.setDefaultEditor(Cell.class, new MyTableCellEditor());
+1

Source: https://habr.com/ru/post/1702220/


All Articles