使用不同班级的表 [英] using a table in a different class

查看:33
本文介绍了使用不同班级的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类中创建了一个 JTable,现在我需要使用同一个表,在不同的类中设置一些值.我将如何在不同的类中使用相同的 JTable.请告诉.

I have created a JTable in one class now i need to use the same table, to set some value in it, in a different class. how shall i use the same JTable in that different class. please tell.

推荐答案

您不需要第二个类来访问实际的表,只需访问底层的 TableModel.这可以通过多种方式实现:

You don't need the second class to have access to the actual table, only to the underlying TableModel. This can be achieved in many ways:

  • 第一个类中的公共方法 getTableModel(),第二个类可以使用该方法获取对模型的引用
  • 两个类都保留对创建其实例时设置的模型的引用
  • 第一个类中的一个公共方法 addValue() 获取值并将其添加到表模型中,而不暴露模型本身.如果您只需要执行非常具体的操作(例如添加值),这是最好的解决方案.
  • A public method getTableModel() in the first class which the second can use to get a reference to the model
  • Both classes keep a reference to the model that is set when their instances are created
  • A public method addValue() in the fist class that takes the value and adds it to the table model without exposing the model itself. This is the best solution if you only need to perform very specific operations like adding values.

哪种方法最适合您是设计问题,取决于您的具体情况.

Which method is best suited for you is a matter of design depends on your specific scenario.

这篇关于使用不同班级的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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