I would appreciate it a lot if someone enlightened me on what is the "proper" way to build a java application which utilizes the Swing. How you navigate through the views of the program? To further elaborate on what I have in mind: I need a menubar at the top of my Frame and when the user selects a menu item the application should present him the right view. These "views", are they panels? How do you create such an application? I have tried with panels and show, hide functions but I am not pleased with the result. Is this the proper way to build such an application? Looking forward to your answers guys! Thanks a lot in advance!
-
I used https://java.net/projects/appframework/ with good success. I'm interested to know if anyone has used CDI or dependency injection combined with some sort of framework... – Jonathan S. Fisher Aug 11 '13 at 16:48
-
Have you tried `CardLayout` for your "views"? [http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html] – PM 77-1 Aug 11 '13 at 17:01
-
For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). In your use-case, each panel would be one card. – Andrew Thompson Aug 11 '13 at 17:03
-
I haven't tried CardLayout but it seems a good idea! Thank you very much! If anyone has something different to suggest, please do! – Chris Aug 11 '13 at 17:27
2 Answers
You can use a JFrame.On the top of it, have a menu bar, you can add items to it.You can also add sub menu to items.You can perform action on its event.Netbeans provide a easy way, but it is not recommended from coding point of view.

- 2,274
- 3
- 15
- 18
If it is a simple application, JFrame and JMenuBar is enough. If it is built out of a number of screens, as your question suggests, then CardLayout can be useful to manage which screen is showing. Building those out of individual JPanels is a good idea.
There are various GUI builders built into Java IDEs which can help you design those panels and wire them up to your code that implements the logic of the application.
How the pieces of the application communicate with each other is up to you; the typical pattern is to have a "model" class for each one which populates the models of the individual components.
If it is a very complex application, you may want to use a framework which takes care of the plumbing of a desktop application, such as the NetBeans Platform or other similar frameworks.

- 1,741
- 11
- 13