简体   繁体   English

Java数据绑定最佳实践

[英]Java Data binding best practices

Even if I'm not new to Java, I've only used it in school/university environment so I don't know all the best practices used in the enterprise. 即使我不是Java新手,我也只是在学校/大学环境中使用它,所以我不知道企业中使用的所有最佳实践。

In particular I'm now developing a Java desktop application (using Swing at the UI Layer) and I'm particularly interested in best practices about data binding with swing components. 特别是我现在正在开发一个Java桌面应用程序(在UI层使用Swing),我对使用swing组件进行数据绑定的最佳实践特别感兴趣。

For example I could have a List of model objects at the model layer and I need to show them in a JTable. 例如,我可以在模型层有一个模型对象列表,我需要在JTable中显示它们。 Then, when a single row of the JTable is selected, I need to display some information regarding the model object corresponding to the selected row on some JLabels. 然后,当选择JTable的单行时,我需要在一些JLabel上显示与所选行对应的模型对象的一些信息。

What libraries should I use? 我应该使用哪些库? What are the best practices to do so? 这样做的最佳做法是什么?

I'm looking for some links/articles/tutorials(/books?) to dive into this topic and to learn about pros and cons of the various solutions. 我正在寻找一些链接/文章/教程(/书籍?)来深入研究这个主题,并了解各种解决方案的优缺点。

For the specific example you give I would recommend the following approach: 对于您给出的具体示例,我建议采用以下方法:

  1. Represent your model objects as a List<Model> where the List implementation supports RandomAccess (eg an ArrayList ). 将模型对象表示为List<Model> ,其中List实现支持RandomAccess (例如ArrayList )。
  2. Subclass AbstractTableModel and override getValueAt(int row, int col) to index into your List<Model> and retrieve the appropriate Model instance. 子类AbstractTableModel并覆盖getValueAt(int row, int col)以索引到List<Model>并检索相应的Model实例。 Then return the particular attribute you wish to bind to column: col . 然后返回要绑定到column: col的特定属性。

In general I roll my own when it comes to data binding rather than use a framework. 总的来说,在涉及数据绑定而不是使用框架时,我会自己动手。 For editor-style panels I typically implement three methods: initialise() , commit() and clear() , whereby initialise takes an instance of the domain object being edited, commit applies any changes and returns a modified instance of the domain object, and clear clears all UI component values or sets them back to default values. 对于编辑器样式的面板,我通常实现三个方法: initialise()commit()clear() ,其中initialise采用正在编辑的域对象的实例, commit应用任何更改并返回域对象的修改实例,以及clear清除所有UI组件值或将其设置回默认值。

Always have your application open for expansion and keep in mind the coupling factor. 始终打开应用程序进行扩展,并牢记耦合因素。 What I mean by that is you should be storing all your datamodel in separate datastructures irrespective of the UI layer (Swing components). 我的意思是你应该将所有数据模型存储在不同的数据结构中,而不管UI层(Swing组件)。 You should try to have a separate loosely coupled (have a separate data provider package) data structure that would give you all the values that is needed. 您应该尝试使用单独的松散耦合(具有单独的数据提供程序包)数据结构,该数据结构将为您提供所需的所有值。 Once this design is in place, you can start worrying about your UI bindings. 一旦设计到位,您就可以开始担心UI绑定了。

Have a neat UI IDE (like Netbeans) for your swing related development. 拥有一个简洁的UI IDE(如Netbeans),用于与swing相关的开发。 ALWAYS go for layouts . 总是去布局 Layouts are hard to design at first but once you get to work with them, it will be really useful and handy. 布局一开始很难设计,但是一旦你开始使用布局,它将非常有用和方便。 You will further have to understand listeners in Swing and ofcourse Swingworkers. 你将进一步了解Swing和当然Swingworkers的听众。 Do a google, you will find really good resources on these. 做一个谷歌,你会发现这些非常好的资源。

To handle the selection action, you will need to implement ListSelectionListener on the selection model of your JTable . 要处理选择操作,您需要在JTable的选择模型上实现ListSelectionListener You can get some details here and here . 你可以在这里这里得到一些细节。

Once the event is fired, you will be provided with some data on the even, including the source of the event. 一旦事件被触发,您将获得有关偶数的一些数据,包括事件的source You will also be provided with the leading selection index, which you can use to identify the selected row (assuming you are not allowing multiple selection). 您还将获得前导选择索引,您可以使用该索引来标识所选行(假设您不允许多项选择)。 You will have to gather the data object from your table model (or other shared model depending on your model design) in order to get the values for your buttons. 您必须从表模型(或其他共享模型,取决于您的模型设计)收集数据对象,以获取按钮的值。

I dont know swing very well, but have you tried the Model gui mediator pattern? 我不知道摆动很好,但你有没有尝试过模型gui mediator模式? Is applicable in every language and with any gui components. 适用于所有语言和任何gui组件。

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

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