简体   繁体   English

扩展AbstractTableModel时,使单元格在JTable中可见

[英]Making a cell visible in JTable when AbstractTableModel is extended

I have extended the AbstractTableModel to suit my requirements. 我已经扩展了AbstractTableModel以适合我的要求。 Now this table can be altered by other methods of my GUI. 现在可以通过我的GUI的其他方法更改此表。 I want the table to scroll to the currently edited cell into view. 我希望表格滚动到视图中当前编辑的单元格。 To do this, I think I have to first get the JViewport of the current JComponent, but I see no method by which I can achieve this? 为此,我认为我必须首先获取当前JComponent的JViewport,但是我看不到有什么方法可以实现这一目标? How do I achieve this? 我该如何实现?

I have already done this when I have used the default JTable, but how do I do this when we extend AbstractTableModel? 当我使用默认的JTable时,我已经做到了,但是当我们扩展AbstractTableModel时,该如何做呢?

Models are designed to store data and notify views when the data has changed. 模型旨在存储数据并在数据更改时通知视图。 It notifies the view of a change in data by firing events. 它通过触发事件来通知数据变化。 It is the responsibility of the view to listen for these events. 监听这些事件是视图的责任。 Therefore the model never knows directly what view is being updated. 因此,模型永远不会直接知道要更新哪个视图。 This type of functionality should NOT be part of the model. 这类功能不应成为模型的一部分。

One approach might be to use a TableModelListener. 一种方法可能是使用TableModelListener。 You can create a TableModelListner with the table as a paramenter. 您可以使用表作为参数创建TableModelListner。 Then when data is changed the listener will be notified. 然后,当数据更改时,将通知侦听器。 You can then invoke table.scrollRectToVisible(...) on the table. 然后,您可以在表上调用table.scrollRectToVisible(...)。 However, with this approach you can't distinquish between edits that have been applied directly through the TableModel versus udates that have been done through the JTable itself. 但是,使用这种方法,您无法区分直接通过TableModel应用的编辑与通过JTable本身进行的编辑。

You may want to have your table fire an event, and have your parent component listen for that event, and scroll accordingly. 您可能希望表触发事件,并让父组件侦听该事件,然后进行相应滚动。 That way your table doesn't need to know about its parent scroll pane. 这样,您的表就不必知道其父滚动窗格。

You can make use of the EventListenerList in DefaultTableModel to notify any listeners. 您可以使用DefaultTableModel中的EventListenerList通知任何侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM