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

发布于 2024-07-12 19:55:32 字数 562 浏览 3 评论 0原文

我正在使用 Java,我正在尝试使 JTable 中的单元格可编辑。 我的类实现了 TableModel 并扩展了 AbstractTableModel(以便我可以使用方法 fireTableCellUpdated(rowIndex, columnIndex)),并且我已经实现了方法 isCellEditable()setValueAt()。 我将表中的单个单元格表示为 Cell 类的对象。

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

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

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

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.

Please tell me if you need more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走野 2024-07-19 19:55:33

您是否设置了 TableCellRendererTableCellEditor< /code>为您的 JTable

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

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

至于编辑单元格,当您想要使用以下命令编辑单元格时,TableCellEditor 会从 TableModel 接收 ObjectJTable 中。 TableCellEditor 将返回一个 Component,用于使用 getTableCellEditorComponent 方法。

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

推荐阅读:我发现在 Swing 的 JTable 中渲染单元格来自 IBMdeveloperWorks 的组件文章对于学习如何处理 JTable 及其单元格渲染和编辑功能非常有帮助。 特别是,创建自定义渲染器编辑表格单元格部分可能令人感兴趣。

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.

蹲墙角沉默 2024-07-19 19:55:33

您是否使用合适的 TableCellEditor< /code>显示组件进行编辑?

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

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文