我们如何制作一个可编辑的表格?
我应该怎么做才能编辑表格的所有单元格?例如,如果在一个单元格中写着“abc”,我想将其更改为“def”,我该怎么做?
what should I do for editing all the cells of a Table? for example if in a one cell is written "abc", I want to change it to" def", how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
扩展 TableModel 类(通常扩展 AbstractTableModel)并确保 isCellEditable(int row,int col) 对于要编辑的单元格返回 true。
您可以使用 TableModelListener 来监听更改。为了触发事件,您可以使用 fireTableCellUpdated(int row,int col) 或其他几个 fireXXX 方法,其实现已存在于 AbstractTableModel 中。
通常,您可能希望使用自己的自定义渲染器和编辑器来获得更多控制。请参阅 http://java.sun.com/docs/ books/tutorial/uiswing/components/table.html 了解更多详细信息
Extend TableModel class(Generally you extend AbstractTableModel) and make sure that isCellEditable(int row,int col) returns true for the cells that you want to edit.
You can have a TableModelListener for listening to changes. In order to fire events you can use the fireTableCellUpdated(int row,int col) or several other fireXXX methods whose implementation is already present in AbstractTableModel.
Also generally you may want to use your own custom Renderers and Editors in get more control. Refer to http://java.sun.com/docs/books/tutorial/uiswing/components/table.html for more details
如果您只想以编程方式编辑它,则只需更改 TableModel。 TableModel 有一个可以使用的
setValueAt(Object, int, int)
方法。如果您希望用户获得一个文本输入字段来编辑值,那么您可能需要阅读 编辑器。
If you just want to edit it programmatically, then just change the value in your TableModel. A TableModel has a
setValueAt(Object, int, int)
method that you can use.If you want that the user gets a text entry field to edit the value, then you may want to read up on editors.
为了使您的单元格可供用户编辑,您的 TableModel 必须实现一个返回 true 的
public boolean isCellEditable(int row, int col)
方法(对于您希望可编辑的单元格)。 JTable 将通过 public void setValueAt(Object value, int row, int col) 方法自动更新 TableModel;您可能需要使用TableModelListener
来对这些更改做出反应。To make your cell user-editable, your TableModel must implement a
public boolean isCellEditable(int row, int col)
method that returns true (for those cells that you want to be editable). The JTable will automatically update the TableModel trough thepublic void setValueAt(Object value, int row, int col)
method; you might want to use aTableModelListener
to react to those changes.您必须在表中指定给定单元格是否可编辑,您可以在 JTable.isCellEditable(row, col ) 或直接在 TableModel.isCellEditable(row, col) 方法。
这将更改 UI 并让您输入数据:
要获取值,您必须使用: JTable.setValueAt() 或 TableModel.setValueAt() 方法。
为了更好地理解如何使用表、表模型和其他功能,您可以遵循 Java 教程:如何使用Tables很有启发
You have to specify in your table that a given cell will be editable or not, you could do that either in JTable.isCellEditable(row, col ) or directly in the TableModel.isCellEditable(row, col) method.
That will change the UI and let you enter data:
To get the value you have to use either: JTable.setValueAt() or TableModel.setValueAt() methods.
To better understand how to use table, table models, and other features you could follow the Java tutorial: How to use Tables it is enlightening