views:

74

answers:

3

My Java application is not very big, but I would like to do it the "right way". Looking for suggestions to not make a big mess...

I am using Swing, the Swing Application Framework and H2 database.

It is a single frame application with different tabs. Most tabs will reflect some aspects of the data in the database. Some of them will be able to change it. Some tabs will have buttons or actions that will redirect to another tab... that's basically it.

I would like to keep things clean, rather portable and flexible (can enable disable some tabs for example or add some easily).

Here are the questions :

1) How to have tabs "refreshed" when the database content changes ? Implement some Listener interface ? Should I define that interface or there is something already existing I can reuse ?

2) How to "link" the different tabs together so that it is easy to jump from one to another (and may have to carry some argument to setup the destination tab to the right "view")

3) If I set some preference with some filters in another part of the application, how do I share such settings with the whole application ?

And when I ask "how to", I really mean "what is the best way"... I can get that working already... just looking for the right tool to use, design pattern or such.

+1  A: 

There are different strategies of gui application building. Here some article about.

Stas
+1  A: 

If using swing, you can use tabbed pane to have a tabular layout.

Implement a listener (as you are planning). Have a look at observable interface in Java.

You can implement a singleton(eg PreferenceManager class) to hold the preference and access it across the application to access properties.

Nrj
He needs to remember to use the Event Dispatch Thread to modify the GUI elements as well.
Chris Dennett
A: 

Do you really want a push model where the server has to maintain a connection to every client and then update them all when anything changes? This is feasible with a handful of clients, but quickly becomes problematic. It's probably better to use a pull model, where the client manually (periodically?) asks for updates. This is also a good way to use optimistic locking with a last_modified field in the DB. Attempt an update, if it's changed since you last reloaded, warn the user and allow them to overwrite / merge.

As for Swing in general, best practice is to factor everything out that's not GUI related into core classes ('business logic') that fire off events. Register all your Swing classes as the appropriate event listener and update the gui appropriately (SwingUtilities.invokeLater() is your friend here).

AngerClown