Java Swing 中模型组件的多个实例?

发布于 2024-11-18 05:00:01 字数 824 浏览 2 评论 0原文

到目前为止,我为相应的 Java Swing 组件提供了不同的模型类,例如,我为多个 JTable 提供了多个 TableModel。每个JTable都有自己的TableModelTableModel 基于一个对象 (Model),提供所有必需的数据。像这样:

public class MyTableModel extends AbstractTableModel {

Model model;

但现在我想做出改变。我的界面提供了模型的多个实例的可能性。所以我的问题是,我应该做什么?

  • MyTable 实例化多个对象
  • 在用户交互时动态更改对模型的当前引用

所以我面临的基本问题:我想使用相同的 JTable 和相同的 >表模型。我应该使用多个 TableModel 还是应该使用更改对数据源的引用?


类似的问题:

我想提供多个选项卡,它们更改底层模型的实例。不会更改类型,但会更改当前实例 - 也就是说,数据会更改。

我现在应该:

  • 实例化视图组件的多个对象吗?例如,为每个可用模型实例化一个自己的 JTableJPanelJScrollPane 对象?
  • 通过监听选项卡式窗格上的更改事件动态更改底层模型的引用

Until now I had different model classes for the appropriate Java Swing component, for instance I have several TableModel for several JTable. Every JTable has its own TableModel. The TableModel is based on one object (Model), giving all the required data. Something like this:

public class MyTableModel extends AbstractTableModel {

Model model;

But now I would like to make a change. My interface offers the possibility of multiple instances of Model. So my question is, what should I do?

  • instantiate multiple objects from MyTable
  • change dynamically the current reference to the model upon user interaction

So the basic problem I am facing: I want to use the same JTable with the same TableModel. Should I use multiple TableModel or should I use changing references to the data source?


Similar question:

I want to offer multiple tabs, they change the instance of the underlying model. The do not change the type, but the current instance - meaning, the data changes.

Should I now:

  • instantiate multiple objects of the view components? For instance instantiate for every available model an own JTable, JPanel, JScrollPane object?
  • change dynamically by listening to change events on the tabbed pane the reference of the underyling model

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

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

发布评论

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

评论(2

蓝天白云 2024-11-25 05:00:01

如果表的结构保持不变(即相同的列数、相同的标题、相同的列类),并且仅内容发生变化,则应保留相同的模型实例但更改数据(并调用 fireTableDataChanged)。
这将更加高效,并且允许保留当前列顺序、当前排序列等。

如果表的结构完全改变,则更改模型本身可能会更容易。您还可以调用 fireTableStructureChanged,但此方法的 javadoc 表示:

这与调用相同
JTable 上的 setModel(TableModel)

对于选项卡,可以遵循相同的规则。

If the structure of the table stays the same (i.e. same number of columns, same titles, same column classes), and only its content changes, you should keep the same model instance but change the data (and call fireTableDataChanged).
This will be more efficient, and will allow keeping the current column order, the current sorted column, etc.

If the structure of the table completely changes, changing the model itself is probably easier. You could also call fireTableStructureChanged, but the javadoc of this method says :

This is the same as calling
setModel(TableModel) on the JTable

Regarding the tabs, the same rule can be followed.

葬シ愛 2024-11-25 05:00:01

如果您要拥有多个选项卡,其中不同的选项卡具有不同的型号,那么答案很简单,您需要不同的表。每个选项卡至少有一个。

同样,如果您有多个选项卡,那么您还需要多个 JScrollPanes 等。

但是,如果您只需要一个表,那么您可能可以使用单个 JTable 和多个模型(如果您不这样做)做一些定制的桌子。 (参见 mKorbel 的评论)。无论哪种方式,您都可以重复使用相同的 JScrollPane。

If you are going to have multiple tabs with a different tab having a different model then the answer is easy, you need different tables. At least one for each tab.

Again, if you multiple tabs then you will also need multiple JScrollPanes, etc.

However, if you are going to have a single spot for a table, you might be able to get away with a single JTable and multiple models if you aren't doing something custom to the table. (See mKorbel's comment). Either way you could reuse the same JScrollPane.

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