简体   繁体   English

在JTable中显示各种对象的实例变量并对其进行修改

[英]Displaying various objects' instance variables in a JTable and modifying them

I am designing an application that has two widgets: 我正在设计一个有两个小部件的应用程序:

-A list that contains arbitrary objects - 包含任意对象的列表
-A table that displays specific properties of the currently selected object - 显示当前所选对象的特定属性的表

The goal is to be able to pick an object from the list, look at the properties, and modify them as necessary. 目标是能够从列表中选择一个对象,查看属性,并根据需要进行修改。 The list can hold objects of various types. 该列表可以包含各种类型的对象。

So say the list contains Vehicle objects and Person objects 所以说列表包含Vehicle对象和Person对象

public class Person
{
  public String     name;
  public Integer    age;
}

public class Vehicle
{
  public String    make;
  public String    model;
}

If I click on a Person object, the table will display the name and age, and I can assign new values to them. 如果我单击Person对象,该表将显示名称和年龄,我可以为它们分配新值。 Similarly, if I click on a Vehicle object, it will display the make and model in the table and allow me to modify them. 同样,如果我单击Vehicle对象,它将在表格中显示品牌和型号,并允许我修改它们。

I have considered writing a method like 我考虑过编写类似的方法

public String[] getFields()
{
   return new String[] {"name", "age"};
}

Which returns a list of strings that represent the instance variables I want to look at, and use some reflection methods to get/set them. 它返回一个表示我想要查看的实例变量的字符串列表,并使用一些反射方法来获取/设置它们。 I can define this getFields method in all of the classes so that I can use the table to handle arbitrary objects that might be thrown into the list. 我可以在所有类中定义这个getFields方法,以便我可以使用该表来处理可能被抛入列表的任意对象。

But is there a way to design this so that I don't resort to reflection? 但有没有办法设计这个,这样我就不会诉诸于反思? The current approach seems like bad design. 目前的方法似乎是糟糕的设计。

On the other hand, I could create multiple TableModel objects, one for every possible class. 另一方面,我可以创建多个TableModel对象,每个可能的类对应一个。 The table would know what rows to display and how to access the object's instance variables. 该表将知道要显示的行以及如何访问对象的实例变量。 But then everytime a new class is added I would have to define a new table model, which also sounds like a weak design. 但是每次添加新类时我都要定义一个新的表模型,这听起来像是一个弱设计。

You have a class (Vehicle) and you know the names of some properties (make, model) that you want to be able to manipulate dynamically for an instance of this class through a JTable UI. 你有一个类(Vehicle),你知道你希望能够通过JTable UI为这个类的一个实例动态操作的一些属性(make,model)的名称。

You have various different approaches to chose from. 您有各种不同的方法可供选择。

A. Use the reflection API A.使用反射API

This is what the reflection API is made for. 这就是反射API的用途。 If you want something so dynamic, there is nothing wrong with using reflection. 如果你想要一些如此动态的东西,使用反射没有任何问题。 The performance overhead will not be significant for this use case. 对于此用例,性能开销不会很大。

B. Use a library like beanutils that is based on the reflection API B.使用基于反射API的beanbeanutils

This should be easier than directly using the reflection API, but it has the drawback that you need to include another dependency in your project. 这应该比直接使用反射API更容易,但它的缺点是您需要在项目中包含另一个依赖项。

C. Create dynamically at runtime the different TableModel classes. C.在运行时动态创建不同的TableModel类。

You can do this using either the java compiler API or javassist . 您可以使用java编译器APIjavassist来完成此操作。 Based on information available at runtime, you are able to compile a new class for each different type of table model. 根据运行时可用的信息,您可以为每种不同类型的表模型编译新类。 If you follow this approach you must be aware that the creation of the class is a heavy task, so the first time you create a TableModel the application will take some time to respond. 如果您遵循这种方法,您必须意识到创建类是一项繁重的任务,因此第一次创建TableModel ,应用程序将需要一些时间来响应。

What to chose? 选择什么?

Of course this is your decision. 当然这是你的决定。 For the specific use case, the overhead added by reflection or beanutils is insignificant, so probably it is better to chose between A or B. In another use case where performance is more critical, then you could examine the C approach, without forgetting the class creation response time problem. 对于特定用例,反射或beanutils添加的开销是微不足道的,因此最好在A或B之间进行选择。在性能更为关键的另一个用例中,您可以检查C方法,而不会忘记类创建响应时间问题。

EDIT: I just realized that in this specific use case there is another important functionality required. 编辑:我刚刚意识到在这个特定的用例中还需要另一个重要的功能。 Convert from String to the appropriate data type of each property and vice cersa. 从String转换为每个属性和副cersa的相应数据类型。 Beanutils has perfect support for that, so it gets a plus here. Beanutils对此有完美的支持,所以它在这里得到了加分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM