我们如何制作一个可编辑的表格?

发布于 2024-08-12 09:39:22 字数 65 浏览 2 评论 0原文

我应该怎么做才能编辑表格的所有单元格?例如,如果在一个单元格中写着“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 技术交流群。

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

发布评论

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

评论(4

皓月长歌 2024-08-19 09:39:22

扩展 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

聆听风音 2024-08-19 09:39:22

如果您只想以编程方式编辑它,则只需更改 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.

神妖 2024-08-19 09:39:22

为了使您的单元格可供用户编辑,您的 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 the public void setValueAt(Object value, int row, int col) method; you might want to use a TableModelListener to react to those changes.

凉宸 2024-08-19 09:39:22

您必须在表中指定给定单元格是否可编辑,您可以在 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

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