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

发布于 2024-10-09 08:17:35 字数 227 浏览 0 评论 0原文

我已经扩展了 AbstractTableModel 以满足我的要求。现在这个表可以通过我的 GUI 的其他方法来更改。我希望表格滚动到当前编辑的单元格进入视图。为此,我想我必须首先获取当前 JComponent 的 JViewport,但我没有看到可以实现此目的的方法?我该如何实现这一目标?

当我使用默认的 JTable 时我已经做到了这一点,但是当我们扩展 AbstractTableModel 时我该如何做到这一点呢?

I have extended the AbstractTableModel to suit my requirements. Now this table can be altered by other methods of my 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? 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

聊慰 2024-10-16 08:17:35

模型旨在存储数据并在数据更改时通知视图。它通过触发事件来通知视图数据的更改。监听这些事件是视图的责任。因此,模型永远不会直接知道正在更新哪个视图。这种类型的功能不应成为模型的一部分。

一种方法可能是使用 TableModelListener。您可以使用表作为参数创建一个 TableModelListner。然后,当数据发生更改时,侦听器将收到通知。然后,您可以在表格上调用 table.scrollRectToVisible(...) 。然而,使用这种方法,您无法区分直接通过 TableModel 应用的编辑和通过 JTable 本身完成的更新。

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. You can create a TableModelListner with the table as a paramenter. Then when data is changed the listener will be notified. You can then invoke table.scrollRectToVisible(...) on the table. 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.

欢你一世 2024-10-16 08:17:35

您可能希望让您的表触发一个事件,并让您的父组件侦听该事件,并相应地滚动。这样您的表就不需要知道其父滚动窗格。

您可以使用 DefaultTableModel 中的 EventListenerList 来通知任何侦听器。

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.

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