使用 Swing 在 GUI 中显示类内容
我有一些带有字段和 getter/setter 的类,我想在 Swing 应用程序中显示该类的字段。
每个字段的名称应显示在Label
中,并且应根据值的类型显示值,即String
使用TextField
>、List
使用ComboBox
等等。
一个困难是类可以包含字段,而字段本身也需要以这种方式处理。
有推荐/标准的方法吗?
我研究了一点 java.beans,但我不太确定当类本身是 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, i. e. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种选择是使用 L2FProd 中的 PropertySheet 组件。从你的描述来看,它应该完全符合你的要求。
很久没有更新了,但是效果很好。
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.
我认为没有一种自动化的方法可以实现这一点。事实上,我认为你甚至可能错误地对待它。例如,如果 List 包含字符串以外的对象,JComboBox 将如何解决这个问题?我建议只使用
JTable
并将名称放在第一列中,将值放在第二列中。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.您可以将
@Annotations
添加到要在 GUI 中检查和显示的字段。在每种情况下,您都必须使用反射来访问和查找要显示的所有字段。我认为您必须向数据绑定添加自定义逻辑,该逻辑检查给定类的字段(可能使用@Display
注释或类似的内容进行过滤)。这应该很容易实现,您可以依赖 POJO Bean 定义并通过 getter/setter 访问所有字段,不要忘记缓存处理的类以避免类的循环依赖。
我希望这有帮助。
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.
有ReflectionUI。它可以仅使用反射来显示原始值对象和列表。它与 getter/setter 属性配合得很好。
There is ReflectionUI. It can display primitive values objects and lists by just using reflection. It works well with getter/setter properties.