views:

144

answers:

4

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.

+2  A: 

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.

Adamski
So, are you saing that I sould create a widget (for example subclassing a JPanel?) which includes all the fields and controls to edit the object, the initialize(), commit() and clear() methods, adding this JPanel subclass to my JFrame layout and then calling the various methods in response to selection events?
Andrea Zilio
In a nutshell - yes. Typically if the JPanel subclass is an editor you may wish to display it in a modal JDialog. I typically add a static factory method on my JPanel that will create a JDialog containing the panel along with OK, Apply and Cancel buttons.
Adamski
+1  A: 

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.

Bragboy
+1  A: 

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.

akf
A: 

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.

Daniele Teti