user2061853
user2061853

Reputation: 777

How to add gap between two buttons in JToolBar?

I need to add a simple gap/space/margin whatever to have space between these two buttons. Unfortunately I can't make it work. Could anyone give me some advice?

It's based on BorderLayout, the buttons are in a JToolBar

enter image description here

Upvotes: 3

Views: 5624

Answers (2)

Kevin Workman
Kevin Workman

Reputation: 42176

What layout is on the JPanel that contains those buttons? You could use a BoxLayout and add Box.createHorizontalStrut() to it.

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
buttonPanel.add(playButton);
buttonPanel.add(previousButton);
buttonPanel.add(Box.createHorizontalStrut(25));
buttonPanel.add(stopButton);
buttonPanel.add(Box.createHorizontalGlue());

Upvotes: 5

Andrew Thompson
Andrew Thompson

Reputation: 168815

See JToolBar.addSeparator() which:

Appends a separator of default size to the end of the tool bar. The default size is determined by the current look and feel.

Or JToolBar.addSeparator(Dimension) which:

Appends a separator of a specified size to the end of the tool bar.

Upvotes: 3

Related Questions