Java Swing中的模型组件有多个实例? [英] Multiple instances of model components in Java Swing?

查看:78
本文介绍了Java Swing中的模型组件有多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到现在,我为适当的Java Swing组件提供了不同的模型类,例如,对于多个JTable,我具有多个TableModel.每个JTable都有其自己的TableModel. TableModel基于一个对象(Model),提供所有必需的数据.像这样:

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;

但是现在我想进行更改.我的界面提供了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?

  • 实例化MyTable
  • 中的多个对象
  • 在用户交互时动态更改当前对模型的引用
  • instantiate multiple objects from MyTable
  • change dynamically the current reference to the model upon user interaction

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

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?

类似的问题:

我想提供多个选项卡,它们更改了基础模型的实例.不会更改类型,但会更改当前实例-意味着数据会更改.

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.

我现在应该:

  • 实例化视图组件的多个对象?例如,为每个可用模型实例化一个自己的JTableJPanelJScrollPane对象?
  • 通过侦听标签式窗格上的更改事件来动态更改底层模型的引用
  • 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

推荐答案

如果您要使用多个选项卡,而另一个选项卡的模型不同,那么答案很简单,那么您就需要不同的表.每个标签至少一个.

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.

同样,如果您有多个标签,则还需要多个JScrollPanes,等等.

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

但是,如果您要在一个表上放一个位置,那么如果您不对表做任何定制的话,就可以摆脱一个JTable和多个模型的困扰. (请参阅mKorbel的评论).无论哪种方式,您都可以重用相同的JScrollPane.

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.

这篇关于Java Swing中的模型组件有多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆