1. About Struts & Glues
One can use Struts and Glues with the BoxLayout. They both help in aligning the components based on available empty space. One can imagine strut and glue both as soft & sticky material which can expand and compress. This factor allows filling up the empty space in the container there by aligning the components. Before we proceed further, have a look at the below picture:
The above picture divides into three sections to study the behaviour of the invisible items: 1) Rigid Area 2) Glue 3) Strut. Note, for better result, all these components should be used with BoxLayout only. Now let us explore these components one-by-one.
1.1 Rigid Area
The Gray area shows the sticky substance which is invisible to the user. In the bottom right, we have a Rigid Area which can be specified in pixels. Rigid Area requires both height and width. The Rigid Area is fixed, and it will not expand when the container resizes. In this example, we will explore frequently used components Glue & Strut.
1.2 Strut
In the bottom left, you can see a strut component used with a vertical BoxLayout. As shown in the picture, the strut will expand either in horizontal direction or in vertical direction. In the above picture, the strut is fixed in height when we place it on Vertical Box, and it can expand width-wise when the user resizes the container. Likewise, a strut can be placed on the Horizontal BoxLayout as well. In that case, the strut will have fixed width and variable height.
1.3 Glue
A Glue is a more flexible alignment component, and it can expand both on X-Axis & Y-Axis, as shown in the picture above. The top portion of the picture shows three usages of Glue in the Vertical BoxLayout.
In the first part, we add a glue after adding button1 and button 2. Button3 comes after the Glue. BoxLayout gives all the free space to the Glue so that it expands to occupy all the available space after adding Button1 and Button2. This makes button3 stay on the bottom edge of the Box.
In the second part, we added the Glue after adding the button1. So now the Glue expands to occupy the free space, pushing button2 and button3 towards the bottom of the Vertical BoxLayout.
Final part shows how two glues are placed between button1 & button2 as well as button2 & button3. Now these two glues together pack all three buttons equally spaced. The BoxLayout will maintain the equal space even when the user resizes the container.
2. About the Glue & Strut Example
Have a look at the below depiction which shows the examples which we will create in the coming sections:
Here, we will create two examples. One on the left, which makes use of Glue component and the one on the right uses the struts. In both the examples, we have three JPanels with BoxLayout. Two JPanels use Vertical BoxLayout and one JPanel in the Frame’s top uses horizontal BoxLayout. The JPanel contents are below:
- Right Panel: Button1, Button2, Button3
- Left Panel: Button7, Button8, Button9
- Top Panel: Button4, Button5, Button6
With these two examples, we can study how the Struts and Glues behave when the containing container resizes. Note, to study the resizing of Glues and Struts, we will use the edges of the BorderLayout.
The below YouTube video explains about Strut, Glues and RegidArea:
3. Glue Grouping Controls
Below is the first example which shows how the glues are used. In line 31, we created a Vertical glue via createVerticalGlue method call. We kept this glue between Button2 & Button3. In the example screenshot, you can see how it pushes the button3 towards the bottom of the Panel. When the JPanel size changes, the glue extends vertically or simply glue height increases.
In line 52, we placed the glue between button 7 and button8. After adding button8 to the JPanel, we added button9. Now, since the glue is between button7 and button8, the Glue pushes both Button 8&9 towards the bottom of the panel.
At Line 41, we created a horizontal glue through the method call createHorizontalGlue. The horizontal glue is fixed in height, but it can expand in width. In our example, we added this Glue between button 4 and 5. So the BoxLayout manager pushes the button 5,6 towards the right edge of the JPanel, which was added to the north portion of the JFrame’s BorderLayout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.HeadlessException; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class BoxLayoutExample2 extends JFrame { public BoxLayoutExample2(String title) throws HeadlessException { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 03: Left Vertical JPanel PanelY1 = new JPanel(); PanelY1.setLayout(new BoxLayout(PanelY1, BoxLayout.Y_AXIS)); JButton btn1 = new JButton("Button 1"); JButton btn2 = new JButton("Button 2"); JButton btn3 = new JButton("Button 3"); PanelY1.add(btn1); PanelY1.add(btn2); PanelY1.add(Box.createVerticalGlue()); PanelY1.add(btn3); //Sample 02: Mid Panel Hirizontal JPanel PanelX = new JPanel(); PanelX.setLayout(new BoxLayout(PanelX, BoxLayout.X_AXIS)); JButton btn4 = new JButton("Button 4"); JButton btn5 = new JButton("Button 5"); JButton btn6 = new JButton("Button 6"); PanelX.add(btn4); PanelX.add(Box.createHorizontalGlue()); PanelX.add(btn5); PanelX.add(btn6); //Sample 04: Right Panel Vertical JPanel PanelY2 = new JPanel(); PanelY2.setLayout(new BoxLayout(PanelY2, BoxLayout.Y_AXIS)); JButton btn7 = new JButton("Button 7"); JButton btn8 = new JButton("Button 8"); JButton btn9 = new JButton("Button 9"); PanelY2.add(btn7); PanelY2.add(Box.createVerticalGlue()); PanelY2.add(btn8); PanelY2.add(btn9); //Sample 05: Add All three Boxed Panels to Frame Window //Managed by Flow Layout ControlHost.add(BorderLayout.EAST, PanelY1); ControlHost.add(BorderLayout.NORTH, PanelX ); ControlHost.add(BorderLayout.WEST, PanelY2); } } |
4. Glues Spacing Controls Equally
In the previous code, we add more glues By adding/modifying code at Line Nos – 8,10,20,22,32,34. Now, the controls are spaced equally on the BoxLayout by means of more than one Glue. The modified code is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//Sample 03: Left Vertical JPanel PanelY1 = new JPanel(); PanelY1.setLayout(new BoxLayout(PanelY1, BoxLayout.Y_AXIS)); JButton btn1 = new JButton("Button 1"); JButton btn2 = new JButton("Button 2"); JButton btn3 = new JButton("Button 3"); PanelY1.add(btn1); PanelY1.add(Box.createVerticalGlue()); PanelY1.add(btn2); PanelY1.add(Box.createVerticalGlue()); PanelY1.add(btn3); //Sample 02: Mid Panel Hirizontal JPanel PanelX = new JPanel(); PanelX.setLayout(new BoxLayout(PanelX, BoxLayout.X_AXIS)); JButton btn4 = new JButton("Button 4"); JButton btn5 = new JButton("Button 5"); JButton btn6 = new JButton("Button 6"); PanelX.add(btn4); PanelX.add(Box.createHorizontalGlue()); PanelX.add(btn5); PanelX.add(Box.createHorizontalGlue()); PanelX.add(btn6); //Sample 04: Right Panel Vertical JPanel PanelY2 = new JPanel(); PanelY2.setLayout(new BoxLayout(PanelY2, BoxLayout.Y_AXIS)); JButton btn7 = new JButton("Button 7"); JButton btn8 = new JButton("Button 8"); JButton btn9 = new JButton("Button 9"); PanelY2.add(btn7); PanelY2.add(Box.createVerticalGlue()); PanelY2.add(btn8); PanelY2.add(Box.createVerticalGlue()); PanelY2.add(btn9); |
When we run the sample, it will look like below and you can see how the space is divided for the glues equally:

5. Struts Fixing Control Position
Now we will go ahead with the struts. Here, we create a new example BoxLayoutExample3. For left side JPanel which is managed by Vertical BoxLayout, we add two vertical struts at line number 30,32 by using the method createVerticalStrut. The first strut has a fixed height of 20 and the second vertical strut has a fixed height of 40. This means, the strut can expand width-wise, but they are fixed in height. Because of this reason, the controls maintain its position. The same way, the struts at line numbers 54 & 56 work.
For the JPanel called PanelX
, we create horizontal strut by calling the method
createHorizontalStrut. These struts are fixed in width, but can expand in height. The first strut has a fixed width of 20 and the second one has a fixed width of 40.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.HeadlessException; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class BoxLayoutExample3 extends JFrame { public BoxLayoutExample3(String title) throws HeadlessException { //Sample 01: Set Size and Position super(title); setBounds(100, 100, 450, 250); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 03: Left Vertical JPanel PanelY1 = new JPanel(); PanelY1.setLayout(new BoxLayout(PanelY1, BoxLayout.Y_AXIS)); JButton btn1 = new JButton("Button 1"); JButton btn2 = new JButton("Button 2"); JButton btn3 = new JButton("Button 3"); PanelY1.add(btn1); PanelY1.add(Box.createVerticalStrut(20)); PanelY1.add(btn2); PanelY1.add(Box.createVerticalStrut(40)); PanelY1.add(btn3); //Sample 02: Mid Panel Horizontal JPanel PanelX = new JPanel(); PanelX.setLayout(new BoxLayout(PanelX, BoxLayout.X_AXIS)); JButton btn4 = new JButton("Button 4"); JButton btn5 = new JButton("Button 5"); JButton btn6 = new JButton("Button 6"); PanelX.add(btn4); PanelX.add(Box.createHorizontalStrut(20)); PanelX.add(btn5); PanelX.add(Box.createHorizontalStrut(40)); PanelX.add(btn6); //Sample 04: Right Vertical JPanel PanelY2 = new JPanel(); PanelY2.setLayout(new BoxLayout(PanelY2, BoxLayout.Y_AXIS)); JButton btn7 = new JButton("Button 7"); JButton btn8 = new JButton("Button 8"); JButton btn9 = new JButton("Button 9"); PanelY2.add(btn7); PanelY2.add(Box.createVerticalStrut(50)); PanelY2.add(btn8); PanelY2.add(Box.createVerticalStrut(10)); PanelY2.add(btn9); //Sample 05: Add the the Panel to Test Struts ControlHost.add(BorderLayout.EAST, PanelY1); ControlHost.add(BorderLayout.NORTH, PanelX ); ControlHost.add(BorderLayout.WEST, PanelY2); } } |
6. YouTube Demo – Code Implementation
Categories: Swing
Tags: createHorizontalGlue, createHorizontalStrut, createVerticalGlue, createVerticalStrut, Glues, RigidArea, Struts