BoxLayout using vertical and horizontal glue

Below is the code, which i got from Creating two buttons at bottom left/right corner and i have included my attempt,(adding jpanel2)

import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ButtonsLeftAndRight { private JFrame frame; private JPanel pane; private JButton button1; private JButton button2; public static void main(String[] args) { SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui); } public void createAndShowGui() { frame = new JFrame(getClass().getSimpleName()); pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS)); button1 = new JButton("Button1"); button2 = new JButton("Button2"); pane.add(button1); pane.add(Box.createHorizontalGlue()); pane.add(button2); frame.add(pane, BorderLayout.SOUTH); JButton b1 = new JButton ("A"); JButton b2 = new JButton ("B"); JButton b3 = new JButton ("C"); JPanel panel2 = new JPanel(); panel2.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel2.add(b1); panel2.add(Box.createVerticalGlue()); panel2.add(b2); panel2.add(Box.createVerticalGlue()); panel2.add(b3); frame.add(panel2, BorderLayout.CENTER); frame.setSize(500, 500); frame.setVisible(true); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
}

The problem is that BoxLayout can't be shared but i need to somehow do it so that i can get the vertical boxes

I'm trying to create BoxLayout GUI which involves using buttons and JLabels. In the below ASCII B represents buttons an J represents JLabel. Also im only keeping a bit space between buttons and JLabels

__________________________
| |
| B J |
| B J |
| B J |
| J |
| |
|Button1 Button2 |
|________________________|
11

2 Answers

You can have many JPanels, each with a different layout.

For this case I can imagine having GridBagLayout for the panel with buttons and labels which will be centered in the JFrame's BorderLayout.CENTER alignment and having the buttons at the bottom with BoxLayout orientation

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class ButtonsLeftAndRight { private JFrame frame; private JPanel bottomPane; private JPanel centerPane; private JButton button1; private JButton button2; public static void main(String[] args) { SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui); } public void createAndShowGui() { frame = new JFrame(getClass().getSimpleName()); bottomPane = new JPanel(); bottomPane.setLayout(new BoxLayout(bottomPane, BoxLayout.LINE_AXIS)); button1 = new JButton("Button1"); button2 = new JButton("Button2"); bottomPane.add(button1); bottomPane.add(Box.createHorizontalGlue()); bottomPane.add(button2); frame.add(bottomPane, BorderLayout.SOUTH); JButton b1 = new JButton("A"); JButton b2 = new JButton("B"); JButton b3 = new JButton("C"); centerPane = new JPanel(); centerPane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; centerPane.add(b1, gbc); gbc.gridx = 2; centerPane.add(new JLabel("Label 1"), gbc); gbc.gridx = 0; gbc.gridy++; centerPane.add(b2, gbc); gbc.gridx = 2; centerPane.add(new JLabel("Label 2"), gbc); gbc.gridx = 0; gbc.gridy++; centerPane.add(b3, gbc); gbc.gridx = 2; centerPane.add(new JLabel("Label 3"), gbc); gbc.gridx = 1; gbc.gridy++; centerPane.add(new JLabel("Centered Label"), gbc); frame.add(centerPane, BorderLayout.CENTER); frame.add(bottomPane, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
}

We have the labels positioned in the gridx = 2 so we can have the bottom label in the gridx = 1 position and this provides the extra space between the JLabels and the JButtons with the space equals to the length of the bottom label.

This gives us the following GUI before and after resizing:

enter image description here

enter image description here

Of course there might be some other ways of doing it, however this is the first one I thought


Note

  • You added setVisible(true) twice, once is enough
  • frame.setSize(500, 500); vs frame.pack(); use one or the other, not both.

Try to make this between adding jbutton and jlabel.

container.add(Box.createRigidArea(new Dimension(5,0)));

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like