4

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.stack.imgur.com/3lXe2.png

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

https://i.stack.imgur.com/wKmAE.png

camickr
  • 321,443
  • 19
  • 166
  • 288
swang
  • 55
  • 4
  • 4
    `GroupLayout` is really only usable with a GUI builder, which locks the code into a particular IDE (e.g. the Netbeans GUI builder is incompatible with the Eclipse GUI builder). So the major question I'd be asking myself before using group layout, is: will any other programmer, or me with another IDE, have to try to understand or update this code? BTW: Provide ASCII art or a simple drawing of the *intended* layout of the GUI. – Andrew Thompson Aug 03 '20 at 13:18
  • I have updated the post with the picture of the general intended layout. For my attempt using Flow and Border, I used border for the JFrame and had a NavyColor JPanel positioned at East, and a standardwhite JPanel to fill in the rest of the space (at center I think), but then when it came to adding the JPanels and the JLabels, it just became a nightmare. It would also be appreciated if some general rules of thumbs of choosing Layouts, for example I read that flow is generally good for horizontal placement of multiple elements, I tried box for vertical, and it just filled everything up... – swang Aug 03 '20 at 13:30

1 Answers1

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you so much for your detailed response (And your recommendations regarding how I should manipulate the layout, exactly what I needed), and thanks for not absolutely blazing me for such a trivial question. I hope my proficiency with layout manipulation will improve with time, I will be sure to give the swing tutorial a detailed read! – swang Aug 03 '20 at 14:30