Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: how do I properly instantiate GUI and pass domain objects?

I have a GUI with nested panels(tabbed with nested panels and etc). I need to pass domain object to deeply nested panel. I can think of two ways:

  • Instantiate all gui objects in one place, like frame class. That would make passing domain objects dead simple, but Frame class will be huge and hardly maintanable.

  • Each panel has its own class, where we instantiate and layout its
    components. Now its easy to maintain and classes are clean, but how
    do I pass down the chain my domain objects? I dont want to chain-pass them through constructors of panels that shouldn't even know their
    existance. And top level panels would have a ton of these objects to start with.

Niether way seems like a soulution. How do you usually aproach this?

like image 695
Boris Mikhaylov Avatar asked Oct 22 '11 22:10

Boris Mikhaylov


People also ask

Is swing good for GUI?

Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is build on top of the AWT API and entirely written in java. It is platform independent unlike AWT and has lightweight components.

What is GUI in Java with examples?

GUI (Graphical User Interface) in Java is an easy-to-use visual experience builder for Java applications. It is mainly made of graphical components like buttons, labels, windows, etc. through which the user can interact with an application. GUI plays an important role to build easy interfaces for Java applications.

What are the GUI components in Java?

Java's GUI components include labels, text fields, text areas, buttons, etc. The Abstract Windowing Toolkit (AWT) also includes containers which can include these components. Containers include frames (windows), canvases (which are used to draw on), and panels (which are used to group components).


1 Answers

When I put together a Java Swing GUI, I have a data model for each major GUI element. Note that this isn't an MVC pattern. This is more like a local MV pattern. If you want, you can consider GUI element listeners as the "controller".

Each panel has its own class, where we instantiate and layout its components. Now its easy to maintain and classes are clean, but how do I pass down the chain my domain objects?

You have the right idea, although you shouldn't have to do much passing.

My JFrame (or JApplet) will have an associated model class of global type fields. An instance of this model class will usually be passed along to the children elements. This is to allow children elements to react properly when a menu option is selected (as an example)

My JPanel(s) will have an associated model class that maintains the state of text or button children elements.

More complicated children elements, like a JList or a JTree, already have an associated data model. I will probably wrap these associated data models into the JPanel model class for convenience.

The children elements will trigger some sort of selection or action listener. Some of these listeners might need access to model classes besides the model class associated with the parent. In this case, you're going to have to pass the instances of your model classes to the listeners.

like image 153
Gilbert Le Blanc Avatar answered Nov 02 '22 20:11

Gilbert Le Blanc