Nikola Božić Nikola Božić - 11 months ago 138
Java Question

How to add vertical space between JButton in BoxLayout?

I have panel with 2 buttons in BoxLayout. What i want is to add vertical space between the buttons.

Sreenshot:

Here is my code:

frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

JPanel panel = new JPanel();
panel.setBounds(new Rectangle(0, 5, 0, 0));
panel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
frame.getContentPane().add(panel, BorderLayout.WEST);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

panel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10) );

JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("New button");
panel.add(btnNewButton_2);

Answer Source

Add an invisible vertical component to the panel in between the two buttons:

JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);

panel.add(Box.createVerticalStrut(50));

JButton btnNewButton_2 = new JButton("New button");
panel.add(btnNewButton_2);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download