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
Making a cell in a JTable editable - the default value of the cell
提问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 TableCellEditor
to 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 TableCellRenderer
and TableCellEditor
for your JTable
?
您设置了TableCellRenderer
和TableCellEditor
你的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.
为了显示一个单元格,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 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.
通过编写自定义单元格渲染器(实现 的类TableCellRenderer
),您将能够使用方法返回Component
要用于显示Cell
对象的getTableCellRendererComponent
。在您的情况下,您可能希望子类化 a JLabel
,它实现TableCellRenderer
并将设置标签的内容以反映您的Cell
对象的内容。
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.
作为编辑单元格时,TableCellEditor
接收Object
从TableModel
当你想在编辑细胞JTable
。在TableCellEditor
将返回Component
其用于编辑单元格内容(Object
使用)getTableCellEditorComponent
方法。
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.
在您提供的情况下,我认为制作一个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 JTable
s 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对学习如何处理JTable
s 及其单元渲染和编辑特性非常有帮助。特别是,创建自定义渲染器和编辑表格单元格部分可能很有趣。