4

I have been writing a database-centric Java Swing application for a while.

The GUI and DAO-code ends up in a big tree like:

JFrame
  |
JTabbedPane
    |
   +--------------------+----------------------+-------------+
   |                    |                      |             |
JPanel1              JPanel2                JPanel3       JPanel4
   |                    |                      |             |
JButtons--JTable1   JTextFields--JButton     JTable2    JDialog--JTable3
             i!          i!                    i!                  i!
           Model1      Model2                Model3              Model4
             |            |                     |                   |
             +------------+-----------+---------+-------------------+
                                      |
                               DataAccessObject

The application has several views, some contains a JTable for showing data, and some contain a dialog with a form for editing or adding data.

I have a DataAccessObject with JDBC-connection. I use several models (extends AbstractTableModel) to connect the views (forms or tables) with the DAO.

In my first version I implemented the DAO as a Singleton, then I learned that this is an anti-pattern and used Dependency Injection instead, so I basically initialize the DAO first and then inject it to the constructor of all the models. Then I initialize the JFrame and inject a reference to the models in the constructor in the hole GUI-tree.

Passing the reference to the models through the hole GUI tree feels very clumsy, but I know that I have good control of dependencies. But is there any better design I could use for a database-centric Java Swing applications with many views of data that needs a model with a connection to the database?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 6
    You're going to need to separate your view from your data. In fact I recommend that you forget about a GUI and instead build a system that manages a database and provides the services and functionality that you desire. Once you've got that, you can build a GUI using Swing that will now use your new system. It's all about layers of abstraction... – Nate W. Sep 08 '10 at 22:56
  • @Shakedown: Isn't that what I have done with my design, where it's only the models that access the DAO? Or do you have a link or example to what you mean? – Jonas Sep 09 '10 at 13:36

1 Answers1

1

I would also support Shakedown's comment. It is all about layers. Separate your code into layers/tiers.

Since you were talking about Dependency Injection I would suggest you to take a look at Spring Rich Client framework to get a feel about how good Swing applications can be designed/developed.

Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51