简体   繁体   中英

Displaying class contents in a GUI with Swing

I have some classes with fields and getters/setters and I want to display the fields of the class in a Swing application.

The name of each field should be dosplayed in a Label and the value should be displayed depending on the type of the value, ie String uses a TextField , List uses a ComboBox and so on.

One difficulty is that the class can contain fields, which also need to be treated this way itself.

Is there a recommended/standard way of doing that?

I looked a bit into java.beans but I'm not really sure if it isn't primarily used when the class is a Swing component itself.

Another option is to use PropertySheet component from L2FProd . From you description it should do exactly what you wanted.

It wasn't updated for a long time but works well.

在此处输入图片说明

I don't think there is an automated way to accomplish this. In fact I think you might even be approaching it incorrectly. What if you List contains objects other than Strings for example, how is a JComboBox going to break this out? I would suggest just using a JTable and putting the name in the first column and the value in a second column.

You could add @Annotations to your fields you want to examine and display in the GUI. In every case you have to use reflections to access and find all the fields you want to display. I think you have to add a custom logic to the databinding which examines a given class for it's fields (maybe filtered with a @Display annotation or stuff like this).

This should be very easy to implement, you can rely on the POJO Bean definition and access all the fields over getter/setter, don't forget to cache the handled classes to avoid circular dependencies of classes.

I hope this helps.

There is ReflectionUI . It can display primitive values objects and lists by just using reflection. It works well with getter/setter properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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