Java 使 JTable 中的单元格可编辑 - 单元格的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/478726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:09:27  来源:igfitidea点击:

Making a cell in a JTable editable - the default value of the cell

javajtable

提问by user42155

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.

我正在使用 Java,并且我正在尝试使 JTable 中的单元格可编辑。我的类实现了 TableModel 并扩展了 AbstractTableModel (以便我可以使用方法fireTableCellUpdated(rowIndex, columnIndex)),并且我已经实现了方法isCellEditable()setValueAt()。我将表中的单个单元格表示为类 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.

现在这是我的问题:单元格已经可编辑,当我单击它时,光标出现在单元格中,但是,单元格中还会出现一个字符串,如下所示:Cell@1e63e3d。我删除此字符串并将值放入单元格中,我想放入该值,然后单击 Enter,它工作正常。但是我希望当我点击那里的单元格时什么也不出现,一个空字符串,而不是Cell@1e63e3d. 而且我不知道如何将这个空字符串设置为默认值以及在哪里。

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

我的 Cell 类存储有关单元格的信息(特征),如单元格的颜色,以及作为实例变量的值。

Please tell me if you need more information.

如果您需要更多信息,请告诉我。

回答by Zach Scrivena

Are you using an appropriate TableCellEditorto display the component for editing?

您是否使用适当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;
    }
}

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());

回答by coobird

Have you set a TableCellRendererand TableCellEditorfor your JTable?

您设置了TableCellRendererTableCellEditor你的JTable

For displaying a cell, the TableCellRendereris used to render the contents for a location from the TableModel. By default, it will use the toStringmethod of the Objectin that location, so that would explain the Cell@1e63e3dbeing displayed in the cell -- that is the result of the toStringmethod being called on your Cellobject.

为了显示一个单元格TableCellRenderer用于从 中呈现某个位置的内容TableModel。默认情况下,它将使用该位置中的toString方法Object,这样就可以解释Cell@1e63e3d单元格中显示的内容——这是toString在您的Cell对象上调用该方法的结果。

By writing a custom cell renderer (a class that implements TableCellRenderer), you'll be able to return a Componentyou want to use to display the Cellobject, using the getTableCellRendererComponentmethod. In your case, you may want to subclass a JLabelwhich implements TableCellRendererand will set the contents of the label to reflect the contents of your Cellobject.

通过编写自定义单元格渲染器(实现 的类TableCellRenderer),您将能够使用方法返回Component要用于显示Cell对象的getTableCellRendererComponent。在您的情况下,您可能希望子类化 a JLabel,它实现TableCellRenderer并将设置标签的内容以反映您的Cell对象的内容。

As for editing a cell, the TableCellEditorreceives the Objectfrom the TableModelwhen you want to edit a cell with in the JTable. The TableCellEditorwill return a Componentwhich is used to edit the cell contents (the Object) using the getTableCellEditorComponentmethod.

作为编辑单元格时,TableCellEditor接收ObjectTableModel当你想在编辑细胞JTable。在TableCellEditor将返回Component其用于编辑单元格内容(Object使用)getTableCellEditorComponent方法。

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

在您提供的情况下,我认为制作一个JTextField实现TableCellEditor接口的方法将能够为您完成这项工作。当您覆盖 时getTableCellEditorComponent,请检查您是否有Cell对象的实例(即object instanceof Cell),如果是这种情况,请初始化您JTextField以包含Cell您要显示或编辑的对象的内容。

Recommended reading:I found the Rendering cells in Swing's JTable componentarticle 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 renderersand Editiing table cellssections may be of interest.

推荐阅读:我发现IBM developerWorks 上Swing 的 JTable 组件文章中的Rendering cells对学习如何处理JTables 及其单元渲染和编辑特性非常有帮助。特别是,创建自定义渲染器编辑表格单元格部分可能很有趣。