Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 493
Andrea Zilio Avatar asked Jun 28 '10 14:06

Andrea Zilio


People also ask

Which is better view binding or data binding?

ViewBinding vs DataBindingThe main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.

Is data binding necessary?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.

Can I use both data binding and view binding?

Conversely, view binding has the following limitations compared to data binding: View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.


1 Answers

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 (e.g. 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.

like image 59
Adamski Avatar answered Oct 14 '22 13:10

Adamski