In this tutorial
You will learn how to...
- How to set the Layout of a JPanel.
- How to position things within each Layout.
- How to size and align things within each Layout.
- How to use each Layout effectively and when.
Layouts - Part 1
So far in our examples, we have been using a system called Absolute Positioning. This involves setting the location and size of every individual widget on the screen. There can be a lot of time-consuming work using this technique especially when it comes to larger, more complex GUIs.
A LayoutManager is a process designed by Sun that automatically controls the size, shape and position of each widget within a container like a JFrame or JPanel. You give each of your widgets a preferred size and which area of the screen you'd like it in and the LayoutManager will do the rest.
LayoutManagers also handle the re-sizing of a frame far better than Absolute Positioning, and you can set behaviours to change the size of widgets and panels depending on the dimensions of the re-size.
Of course, you can provide hints as to how you want objects aligned on the pane, but you can spend your time shaping the GUI instead of moving every single individual widget around.
We can either use Absolute Positioning or not. We cannot have Absolute Positioning if a LayoutManager is used within the program. The default LayoutManager of a JPanel is a FlowLayout which we will look at in the next section.
There are 6 commonly used LayoutManagers in the Swing Toolkit to date (and 2 more complicated ones). The 6 common ones are...
In this tutorial, we will give you a quick summary of each Layout and then go a bit more in depth in each.
The BorderLayout is the default layout for JFrames (which we have seen before), JApplets and JDialogs (which we will see in later tutorials). It is based around a 5-area layout...top, left, bottom, right and centre. When the window is re-sized, any space generated is given to the centre component. If you were to add a button to each of these, it'd look a little like this.
The box layout is there to position widgets in a single row or column. There are some very cool features of the BoxLayout such as Rigid Areas and Invisible Glue that can be used to make the layout just right. Is very good for custom GUIs, not just bog standard ones.
You can stack your widgets on top of each other...
...or align them side-to-side.
Card Layout is a good one for when you want to display panels or containers at different times. With the layout, you can assign an order to panels and use commands like first, next and last to cycle through them.
The yellow box shows you the area that changes in this GUI when the buttons are pressed.
This is the default layout of a JPanel. Simply, this Layout places your widgets one after another until it reaches the end of the Panel then starts another row.
Very simple, but useful if you can control the size of your Panel and widgets.
I reckon this should be called Table Layout instead of Grid, but you know those crazy Americans! Oh what won't they do next!
In this layout, each widget is resized and fitted into a cell. The widget takes up the size of the entire cell, and every cell in the 'grid' is the same size. Quite useful if you know how to control it and handy for displaying things like thumbnails of pictures and photos.
GridBag is an extension of the Grid Layout, and lets you manipulate the cells and positioning to a far greater level. You create a grid but can change the size of each individual cell. Each item in the cell is not automatically placed as it is in GridLayout, you can miss out cells if you wish. You can use it to make a really special looking GUI.
Let's go through each one in order and look at examples.