How to organize a Swing GUI application? How to organize a Swing GUI application? java java

How to organize a Swing GUI application?


Although it is Groovy, not Java, I would advise you take a look at Griffon, which is a "Grails for Swing".

It enforces a given structure (in terms of directories and patterns, MVC in particular) to all applications you build with it.

I think it can give you good ideas in general, although you would have to perform some little adaptation to Java.

Besides, please note that Griffon also supports Application building in Java, and it may also provide "archetypes" for that, so you could check that as well.


You can read A Swing Architecture Overview. But when I do larger Swing applications I don't organize them in the same way as I do when doing web-development. I merely organize my Swing applications in functional parts. E.g. if the app has four tabs with forms and tables, I put the components from one tab in one package. Then I usually ends up with a few custom implementations of AbstractTableModel that the forms and tables interact with. And the Model keeps a cache of the data and communicates with the database.

In my latest Swing application, I used Akka actors to simplify the threading. Then I had all my Swing components executed on the EDT-thread and they communicated with a Data Access Object that was a custom Actor. Then I also had a few TimerTask that synchronized data from the database with services on Internet, but they were never run on the EDT, they only communicated with the DAO, (my Actor).


Web-development forces you to certain source and resource layouts. This is not the case with Swing applications. Here you can use the layout that suits your goals the best.

You will be a lot more productive if you use some IDE. NetBeans is the perfect choice for Swing. NB gives you some restrictions on how to store your classes. There is a "Source Packages" folder which contains the Java Packages and a "Test Packages" for the corresponding JUnit test classes if you use JUnit, which i strongly advise you to do.

In case you use Hibernate, you must stick to its conventions, build a whatever.entity and whatever.util and the configuration XML files need to be in the 'default package'.

Any further organization of these packages is up to programmer. I make a package with the JFrame that runs the whole show. And a package for every logical unit. Sometimes i use a package for the AbstractTableModels and keep them together and another for ComboBoxModels. It is not necessary to make deeper structures. You can have a separate package for the icons you may use for your buttons, one for sounds etc. Netbeans creates the runnable jar file for your application where all necessary files including the needed library jars are included.

Here you have a little example. All names of the packages except 'th' are quite obvious. 'th' is the name of this application and i have the JFrame that hosts the application in there. This application uses iReport in order to generate reports. This is what the 'iReports' package is for.

enter image description here

Books describe smaller samples and are more concerned with the Java conventions.

You could try Apache Maven and see the architecture they provide with the application archetypes.