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.
我已经在一个类中创建了一个JTable,现在我需要使用相同的表,在不同的类中设置一些值。我该如何在不同的类中使用相同的JTable。请告诉。
5 个解决方案
#1
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:
您不需要第二个类可以访问实际表,只需要访问基础TableModel。这可以通过多种方式实现:
- 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.
第一个类中的公共方法getTableModel(),第二个类可用于获取对模型的引用
这两个类都保留对创建实例时设置的模型的引用
第一个类中的公共方法addValue(),它接受值并将其添加到表模型中,而不暴露模型本身。如果您只需要执行非常具体的操作(如添加值),这是最佳解决方案。
Which method is best suited for you is a matter of design depends on your specific scenario.
哪种方法最适合您,设计问题取决于您的具体情况。
#2
Maybe I am too anal when it comes to encapsulation, but I would not expose the TableModel or the JTable itself usually. In the class containing the JTable I would create methods for adding/removing/setting the values of the JTable. If appropriate I might also have the class with the JTable observe a service which might change its data.
也许我在封装时过于肛门,但我不会经常暴露TableModel或JTable本身。在包含JTable的类中,我将创建用于添加/删除/设置JTable值的方法。如果合适,我也可能让JTable的类观察可能改变其数据的服务。
#3
Add a public method in the class that encloses JTable which will set the value and call this method from all the other classes.
在包含JTable的类中添加一个公共方法,该方法将设置值并从所有其他类调用此方法。
#4
If you need to edit values, why don't you work on the Table Model?
如果您需要编辑值,为什么不使用表模型?
JTable.getModel()
(or getTableModel(), I don't remember)
(或getTableModel(),我不记得了)
#5
The class that contains JTable
must expose this field somehow. Alternately and preferably, the class can provide a method to change some value in the JTable
. The other class must reference the class that contains the JTable
(directly or not):
包含JTable的类必须以某种方式公开此字段。或者并且优选地,该类可以提供改变JTable中的一些值的方法。另一个类必须引用包含JTable的类(直接或不直接):
class A {
private JTable myJTable;
public JTable getMyJTable() {
return myJTable;
}
public void setMyJTableValue(Object value) {
// set the value accordingly
}
}
class B {
private A a;
public void methodWithAccessToA() {
// business logic ...
a.setMyJTableValue(myBusinessValue);
// ...
a.getMyJTable().setValue(myBusinessValue);
}
}
#1
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:
您不需要第二个类可以访问实际表,只需要访问基础TableModel。这可以通过多种方式实现:
- 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.
第一个类中的公共方法getTableModel(),第二个类可用于获取对模型的引用
这两个类都保留对创建实例时设置的模型的引用
第一个类中的公共方法addValue(),它接受值并将其添加到表模型中,而不暴露模型本身。如果您只需要执行非常具体的操作(如添加值),这是最佳解决方案。
Which method is best suited for you is a matter of design depends on your specific scenario.
哪种方法最适合您,设计问题取决于您的具体情况。
#2
Maybe I am too anal when it comes to encapsulation, but I would not expose the TableModel or the JTable itself usually. In the class containing the JTable I would create methods for adding/removing/setting the values of the JTable. If appropriate I might also have the class with the JTable observe a service which might change its data.
也许我在封装时过于肛门,但我不会经常暴露TableModel或JTable本身。在包含JTable的类中,我将创建用于添加/删除/设置JTable值的方法。如果合适,我也可能让JTable的类观察可能改变其数据的服务。
#3
Add a public method in the class that encloses JTable which will set the value and call this method from all the other classes.
在包含JTable的类中添加一个公共方法,该方法将设置值并从所有其他类调用此方法。
#4
If you need to edit values, why don't you work on the Table Model?
如果您需要编辑值,为什么不使用表模型?
JTable.getModel()
(or getTableModel(), I don't remember)
(或getTableModel(),我不记得了)
#5
The class that contains JTable
must expose this field somehow. Alternately and preferably, the class can provide a method to change some value in the JTable
. The other class must reference the class that contains the JTable
(directly or not):
包含JTable的类必须以某种方式公开此字段。或者并且优选地,该类可以提供改变JTable中的一些值的方法。另一个类必须引用包含JTable的类(直接或不直接):
class A {
private JTable myJTable;
public JTable getMyJTable() {
return myJTable;
}
public void setMyJTableValue(Object value) {
// set the value accordingly
}
}
class B {
private A a;
public void methodWithAccessToA() {
// business logic ...
a.setMyJTableValue(myBusinessValue);
// ...
a.getMyJTable().setValue(myBusinessValue);
}
}