JTable、TableModel 和 JTable 之间的关系 表数据
JTable
, TableModel
和TableData
? 如果我只是修改 TableData
,是否也会更改 JTable
组件上的数据显示,或者我必须调用某种方法来同步两者。 我看过这个,但它没有明确告诉我或向我展示更新方面的关系
Whats the relationship between a JTable
, TableModel
and TableData
? If i just modify the TableData
, does that also change the data display on the JTable
component or I have to call some method to sync the two. I have looked at this, but it does not explicitly tell or show me the relationship in terms of updates
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您更改 TableModel 数据中的值时,必须触发此事件。 例如。 要更改一个单元格的值,请调用
fireTableCellUpdated(row, col);
。更具体地看看这个。
When you change a value in the TableModel data, you must fire this event. eg. changing the value of one cell, you call
fireTableCellUpdated(row, col);
.Look at this more specifically.
在 JTable 中设置 TableModel 时,表会添加一个观察者 (TableModelListener) 来获取有关模型更改的通知。
当更改模型中的数据时,模型应通过触发 TableModelEvent. 事件本身可以包含有关更改粒度的补充信息(结构是否显着更改,或者仅更改某些单元格数据),以允许 JTable 执行优化的视图更新。
When setting the TableModel in a JTable, the table adds an observer (TableModelListener) to get informed about changes in the model.
When changing data in the model, the model is expected to notify the registered listeners by firing a TableModelEvent. The event itself can contain supplemental information about the granularity of the change (whether the structure significantly changed, or only some cell data is changed) to allow the JTable to do an optimized view update.
我假设您使用短语“TableData”仅表示 JTable 中显示的数据。 要回答您的问题,您是否看到自动更新取决于您的 TableModel 及其与数据的交互方式。
当底层数据模型发生更改时,TableModel 应该触发事件,这反过来会通知 JTable 发生了更改并且应该重新绘制。 所以就关系而言,JTable监听TableModel上的事件变化; TableModel 不了解 JTable。
根据模型的组织方式,其下方的数据可能会在 TableModel 不知情的情况下发生变化。 在这种情况下,如果您有从 TableModel 到数据的直接传递,则当重新绘制自然发生时(屏幕调整大小、鼠标移动等),屏幕上的值将发生变化,但如果您想强制该事件,您应该获得TableModel 通过上述事件通知 JTable。 就关系而言,TableModel知道表数据,但表数据不知道模型(通常)。 TableModel 可能会也可能不会监听数据的变化。
I presume you're using the phrase "TableData" to just mean the data being shown in the JTable. To answer your question, whether you see automatic updates or not depends upon your TableModel and how it interacts with your data.
A TableModel should fire events when the underlying data model has changed, this in turn will notify the JTable that a change has occurred and it should be redrawn. So in terms of a relationship, the JTable listens to event changes on the TableModel; the TableModel has no knowledge of the JTable.
Depending on how your model is organised, the data underneath it could change without the TableModel knowing. In this instance, if you have a direct passthrough from the TableModel to the data, the values onscreen will change when a repaint naturally occurs (screen resize, mouse moving over etc.), but it you want to force the event you should get the TableModel to notify the JTable through the aforementioned events. In terms of relationship, the TableModel knows about the table data, but the table data has no knowledge of model (usually). The TableModel may or may not be listening on the data for changes.