简体   繁体   中英

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.

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.

For example I could have a List of model objects at the model layer and I need to show them in a 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.

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 ).
  2. Subclass AbstractTableModel and override getValueAt(int row, int col) to index into your List<Model> and retrieve the appropriate Model instance. Then return the particular attribute you wish to bind to 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.

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). 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.

Have a neat UI IDE (like Netbeans) for your swing related development. 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. 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 . 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. 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? Is applicable in every language and with any gui components.

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