javaswinglayoutlayout-managergrouplayout

Java UI Layouts Choice


Greetings all, I am relatively new to both UI designing and this community, so take it easy on me.

Summary: I am currently attempting to design a GUI for a native desktop application using Java swing. Like most beginners, I am currently using Netbeans as a UI builder to make the process easier, and I started my process by watching a whole load of Youtube tutorials on how to make a good-looking UI, and I saw many of them use "AbsoluteLayout" and so that's what I did as well.

Efforts: I have built quite a satisfactory UI with all JPanels and JFrames using AbsoluteLayout. The problem is that my "coding veteran" friend, upon inspection of the UI, said that it is absolutely unacceptable to use AbsoluteLayout, and a quick look on this forum and the Oracle Java guides reveals similar sentiments. I have personally tried to present my UI using a mix of BorderLayout and FlowLayout, although it is quite a hassle that I would prefer not to undertake.

Question: Could I use GroupLayout(aka Free Design I think)? I have noticed that it is quite easy to use for a beginner with the help of Netbeans (just drag n drop with not too many restrictions), or does it have a significant disadvantage as well? Is is looked down upon in some way (or considered cheap) if it is used to design a UI instead of the standard Border, Flow, Grid layouts?

Note: It is worth mentioning that the Jframe is undecorated and I do not plan on giving the user the ability to maximize or resize the window (both cruel and lazy of me I know).

Thanks in advance!

My current Gui with AbsoluteLayout:

https://i.sstatic.net/3lXe2.png

My GUI attempt with a mix of Flow and Borderlayout before considering Grouplayout

https://i.sstatic.net/wKmAE.png


Solution

  • The point of using layout managers is the is makes you think logically about how to group all your components and how those components should react when the frame is resized. Even though this application may not resize, you need to know this information for the future.

    Then the layout is typically done by starting with the default BorderLayout of the JFrame and then creating child panel with different layout managers. You can also nest panels for your desired layout.

    So start by reading the Swing tutorial on Layout Manager for the basics of each layout manager.

    So I see:

    1. You start with a JPanel that you can add to the BorderLayout.LINE_START. This panel could use a vertical BoxLayout. You can then add "glue" between each group of components to give spacing as well as a "vertical strut" between each component in a group.

    2. Then you create another panel "center" that again uses a BorderLayout and add this panel to the BorderLayout.CENTER. This panel will have 3 more child panels:

    3. To the "center" panel add a panel to the BorderLayout.PAGE_START. This panel might use a horizontal BoxLayout or FlowLayout.

    4. To the "center" panel add a panel to the BorderLayout.CENTER. This panel could use a GridBagLayout.

    5. To the "center" panel add a panel to the BorderLayout.PAGE_END. This panel will use a GridLayout.

    Yes, it will take a little more time the first few times you design your GUI, but in the long run it is time well spent since you are spending the time learning Java/Swing and not time learning how to use the IDE. You code will be maintainable no matter what IDE you use, instead of being dependent on a given IDE. Use the IDE to help debug etc. but don't let it generate any IDE dependent code.