I am hoping that someone could provide a bit more insight into GridBagLayout.
I have read the Oracle Documentation, but I didn't understand it very well.
I will be very grateful if someone could write something like a 'documentation' based on his personal experience with using GridBagLayout..
Thanks!!!
I've been reading and using a lot about GridBagLayout, and I saw your question as an opportunity to make a post that could help everyone that needs to know how to make JFrames or JDialogs without the "Drag and Drop" GUI some IDES have.
The post will be divided in 3 sections; Basics, Recommendations and Frequent Errors.
Basics
So first of all, you will need to understand what is GridBagLayout.
GridBagLayout is a Layout Manager, and one of the most complex managers.
It's perfect for having total control over how the components are displayed in your Window.
This layout manager works with two very simple variables, gridx and gridy.
Imagine your frame to be something like Excel, the gridx works as the Row variable, and gridy works as the Column variable.
So, gridx corresponds to 1,2,3... and gridy corresponds to A,B,C... (All of this referring to Excel of course).
Now that you know the basics, and have the abstract view needed to design Frames using the GridBagLayout, let's see how to use it for your layout.
You can also do it the following way, but I prefer the previous method.
So now that we have our container with our layout, we need to create our GridBagConstraints. You need the GridBagConstraints not only for creating your gridx/gridy, but also for gridwidth and gridheight.
gridwidth & gridheight tells Java how many rows or columns the object occupies.
The way to create your GridBagConstraints is shown below:
There's also an anchor function, that is used to indicate where the object should start.
For example, in the Center of the Frame or at the Line Start.
There's a variety for anchors, here's all of them:
Take note of how they're organized. This is how each anchor will be positioned in the JFrame.
FIRST_LINE_START....PAGE_START.... FIRST_LINE_END
LINE_START......................CENTER..............LINE_END
LAST_LINE_START........PAGE_END.......LAST_LINE_END
And here's the way to code it:
Finally, there's one other important function, fill.
This function is used for extending the object to the gridsize, and it is very useful when your Windows is resizable.
There are 3 ways to use "fill":
Now, there's one method I want to show you that's very useful. It's especially good if you want to avoid large piles of code when you're adding objects to your container, now you can add one object in one line.
The method is like this:
Here's an example of how the method can be used:
Final Recommendations and Notes
Only declare variables like JPanel as a property of the class when you need them for further things, because they consume memory. JLabels that will be only be used once should be declared inside the constructor, because when the constructor finishes running, the variables that were declared don't exist any more. This is a very good practice that can save memory, which is a really important thing in very large apps that should be able of running in even mediocre PC's.
I recommend you to first fully understand how to use GridBagLayout, and then use the method; remember we mainly use the method for saving time and space.
Common Errors
Not adding width or height when using a method to add the object, will result in a failure, as the object won't appear. I also recommend setting it too although you aren't using the method.
When not using the method, adding an object without putting a "," and then the GridBagConstraints, will cause a logic error, that means the application will run but it will not show the objects.
Resources to write this: Oracle Documentation, my experience and help of others such as @MadProgrammer.