Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GridBagLayout

I've been tasked with designing a basic UI in Java using Swing but I'm stuck with laying it out. I want to create something similar to this but my attempts at using GridBagLayout have resulted in something very messy. Can anyone offer some tips on how I can lay out my GUI like this?

I have a JTabbedPane to which I add two tabs, and to each of those two tabs I add a JPanel containing my controls. How I want to lay it out


2 Answers

I would recommend that

  • the overall GUI use BorderLayout,
  • the JTable be in a JScrollPane and this should be placed BorderLayout.CENTER.
  • The top JPanel holding labels, fields, and buttons be placed BorderLayout.PAGE_START.
  • The top JPanel also can use BorderLayout and can hold the buttons in the BorderLayout.PAGE_END position.
  • The buttons would be held by a GridLayout(1, 0, x, 0) using JPanel where x is the gap between buttons
  • The labels and JTextFields be in a JPanel that uses GridBagLayout and that is placed in the top JPanel in the BorderLayout.CENTER position.
  • that you not follow these recommendations blindly but instead that you experiment with and play with different combinations of nested JPanels, each using its own layout.
  • that you also check out this link
like image 148
Hovercraft Full Of Eels Avatar answered Dec 08 '25 09:12

Hovercraft Full Of Eels


Here's what I would recommend:

  • Use a JPanel pTextBox with GridLayout(3, 2) to hold all of your labels + textboxes
  • Use a JPanel pButtons with GridLayout(1, 3) or BoxLayout(horizontal) hold all of your buttons
  • Use a JPanel pAll with BoxLayout(vertical) to hold pTextBox, pButtons and the Table.
  • Use struts, glues, and min/max/prefererd sizes to adjust spacing / resizing behaviour.

Also check out: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html to compare exactly what you are looking for.

like image 34
sampson-chen Avatar answered Dec 08 '25 10:12

sampson-chen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!