2

How to remove gap between controls in JToolBar? I want to have no free space between controls in JToolBar.

EDIT: I was wrong. There is no free space. The problem is caused by JButton (situated in JToolBar) with icon only. It has some extra margins around the icon. How to remove them?

guest
  • 1,696
  • 4
  • 20
  • 31
  • 1
    There is no gap between controls in a JToolBar. The layout manager for the toolbar is a class that extends BoxLayout which does not add a gap between component unless you add a filler component. If you think there is a gap then post your SSCCE (http://sscce.org) that demonstrates the problems. – camickr Jun 21 '10 at 02:43

2 Answers2

2

The code at http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JToolBar.html explains it with example code:

public class ToolBarButton extends JButton {
  private static final Insets margins = new Insets(0, 0, 0, 0);

  public ToolBarButton(Icon icon) {
     super(icon);
     setMargin(margins);
     setVerticalTextPosition(BOTTOM);
     setHorizontalTextPosition(CENTER);
  }
  // ...

Screen shot:

enter image description here

aioobe
  • 413,195
  • 112
  • 811
  • 826
0

Never tried it myself, but from reading the JavaDoc a bit I would try to things:

  1. Try to use setMargin(Insets) - It does not do what you asked for, but it might have the effect you want.
  2. Set the LayoutManager using setLayout(LayoutManager) and define the padding on the layout manager to be 0. (Perhaps a GridLayout is what you need?)
RonK
  • 9,472
  • 8
  • 51
  • 87