java 如何更改 JTable 单元格中的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/276043/
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
How to change data in JTable cells?
提问by Stella
I can set data in JTable constructor, and then user can change this data when program is running manually(typing from keyboard).
我可以在 JTable 构造函数中设置数据,然后用户可以在程序手动运行时更改这些数据(从键盘输入)。
But what method should I use in case I want to change data in some column? To change column header I use TableColumn method setHeaderValue. What should I use to set value in JTable cell?
但是如果我想更改某些列中的数据,我应该使用什么方法?要更改列标题,我使用 TableColumn 方法 setHeaderValue。我应该使用什么来设置 JTable 单元格中的值?
采纳答案by Paul Tomblin
If you want to allow users to edit the data, then you need to set a TableCellEditor on the cells that you want people to edit. You probably also want to start using a TableModel instead of hard coding the data in the JTable itself.
如果您希望允许用户编辑数据,那么您需要在您希望人们编辑的单元格上设置一个 TableCellEditor。您可能还想开始使用 TableModel 而不是在 JTable 本身中对数据进行硬编码。
See http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
请参阅http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
回答by Vinay Pandey
While creating the JTable you first need to specify that the values of particular column are editable. You can obviously also provide the row basis edit functionality. but all these things you should define while ccreating the table itself. Please reply if you need any help on this.
在创建 JTable 时,您首先需要指定特定列的值是可编辑的。显然,您还可以提供基于行的编辑功能。但是您应该在创建表本身时定义所有这些内容。如果您在这方面需要任何帮助,请回复。

