使 JTable 中的单元格可编辑 - 单元格的默认值
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否设置了
TableCellRenderer
和TableCellEditor< /code>
为您的
JTable
?为了显示单元格,
TableCellRenderer
用于呈现TableModel
中某个位置的内容。 默认情况下,它将使用该位置中Object
的toString
方法,以便解释单元格中显示的Cell@1e63e3d
-- 这是在Cell
对象上调用toString
方法的结果。通过编写自定义单元格渲染器(实现
TableCellRenderer
的类),您将能够返回要用于显示Cell
的Component
code> 对象,使用getTableCellRendererComponent
方法。 在您的情况下,您可能希望子类化JLabel
,它实现TableCellRenderer
并将设置标签的内容以反映Cell
的内容目的。至于编辑单元格,当您想要使用以下命令编辑单元格时,
TableCellEditor
会从TableModel
接收Object
在JTable
中。TableCellEditor
将返回一个Component
,用于使用getTableCellEditorComponent
方法。在您提供的情况下,我认为制作一个实现
TableCellEditor
接口的JTextField
将能够为您完成这项工作。 当您覆盖getTableCellEditorComponent
时,请检查是否有Cell
对象的实例(即object instanceof Cell
),如果是这样,请初始化您的JTextField
包含要显示或编辑的Cell
对象的内容。推荐阅读:我发现在 Swing 的 JTable 中渲染单元格来自 IBMdeveloperWorks 的组件文章对于学习如何处理 JTable 及其单元格渲染和编辑功能非常有帮助。 特别是,创建自定义渲染器和编辑表格单元格部分可能令人感兴趣。
Have you set a
TableCellRenderer
andTableCellEditor
for yourJTable
?For displaying a cell, the
TableCellRenderer
is used to render the contents for a location from theTableModel
. By default, it will use thetoString
method of theObject
in that location, so that would explain theCell@1e63e3d
being displayed in the cell -- that is the result of thetoString
method being called on yourCell
object.By writing a custom cell renderer (a class that implements
TableCellRenderer
), you'll be able to return aComponent
you want to use to display theCell
object, using thegetTableCellRendererComponent
method. In your case, you may want to subclass aJLabel
which implementsTableCellRenderer
and will set the contents of the label to reflect the contents of yourCell
object.As for editing a cell, the
TableCellEditor
receives theObject
from theTableModel
when you want to edit a cell with in theJTable
. TheTableCellEditor
will return aComponent
which is used to edit the cell contents (theObject
) using thegetTableCellEditorComponent
method.In the case you provide, I think that making a
JTextField
which implements theTableCellEditor
interface will be able to do the job for you. When you override thegetTableCellEditorComponent
, check that you have an instance of theCell
object (i.e.object instanceof Cell
) and if that's the case, initialize yourJTextField
to contain the contents of yourCell
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
JTable
s and their cell rendering and editing features. In particular, the Creating custom renderers and Editiing table cells sections may be of interest.您是否使用合适的
TableCellEditor< /code>
显示组件进行编辑?
除了自定义单元格渲染器之外,您还需要告诉您的表使用此自定义单元格编辑器。
Are you using an appropriate
TableCellEditor
to display the component for editing?You will need to tell your table to use this custom cell editor, in addition to the custom cell renderer.