How to organize a Swing GUI application?

JavaSwingUser InterfaceArchitecture

Java Problem Overview


I've written a few GUI's using Swing and I know about MVC, but I never found a good way to really organize my code somehow. What I am looking for is something like the folder structure that maven introduces for each new project. Another example is rails, where MVC is introduced through the folder structure automatically. Is there something similar for Swing?

It would also be nice to see a book that describes the development of a larger Swing Application. All I find are books about design-guidelines where design refers to the look of the application. Other Swing books (like O'Reilly) describe in detail all the swing components, but where is any information about the big picture?

Are there any good examples of a swing gui, where you'd say "That's how you organize code/folders/packages for swing!"?

EDIT: I found the following site http://www.ibm.com/developerworks/java/tutorials/j-springswing/section7.html which describes the usage of spring while creating a GUI. It is a quite old example and it doesn't answer my question, but it is a step into the right direction. It also mentions Spring RCP, but I'm not sure if it could be the solution.

EDIT2: I still didn't find any better answers. Does anybody know an example for a ideally structured Swing GUI which is Open Source? Does anybody know a book, which describes it? And if not for Swing, then maybe for GUI's in general?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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).

Solution 3 - Java

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.

Solution 4 - Java

Unfortunately (or fortunately, it depends how you take a look at it), it is not easy to develop generic panes usable for several data structures. M is often tightly related to V, and V often contains some C.

I tend to put the V pane and the corresponding M structure in the same package. You also need some central controlling displayer to display each individual panes. The C is shared between the central controlling displayer and the panes themselves.

Now, if you need more structure in your project, you could highjack the Struts 2 framework to move all the C in some actions and have panes (the V) indicate what should be the next M/V to display. Struts is originally developed for web applications, but Struts 2 is now detached from the HttpServlet.

I have not tried this myself, but I see some benefits. You could JUnit test screen sequences without actually display screens. Just create panes instances pane and invoke the listening methods with events (mouse click etc...).

Just an idea.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongrackkleView Question on Stackoverflow
Solution 1 - JavajfpoilpretView Answer on Stackoverflow
Solution 2 - JavaJonasView Answer on Stackoverflow
Solution 3 - JavaCostis AivalisView Answer on Stackoverflow
Solution 4 - JavaJérôme VerstryngeView Answer on Stackoverflow