I'm currently trying to create a group of toggle buttons that are similar to those used in the Eclipse formatting settings:

Currently, I have tried to do this as follows:
public class Exercise extends JFrame { private String[] buttonNames = {"A", "B", "C", "D", "E"}; Exercise() { final JPanel topPanel = new JPanel(); topPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); int tabCount = 0; final ButtonGroup topButtonGroup = new ButtonGroup(); for (String buttonName : buttonNames) { JToggleButton tabButton = new JToggleButton(buttonName); topButtonGroup.add(tabButton); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(0, -6, 0, -7);
The result is as follows:

I have a couple of problems with my code. Firstly, I donβt understand why I have to make the inserts negative. According to Oracle's tutorial , "[b] y by default, each component has no external add-on." Therefore, should there be no spaces by default? Without negative investment, the result is as follows:

Secondly, I would like the toggle button to darken instead of blue with the βonβ switch. Is there an easy way to do this through Java Swing? Finally, is there a better approach at all? I am curious to see how Eclipse managed to make the radio buttons look like they are perfectly connected.
Update
I tried using BoxLayout as recommended. Unfortunately, this did not help to solve the problem. The result is almost identical to the above figure. Here is a modified constructor:
Exercise() { final JPanel topPanel = new JPanel(); topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS)); final ButtonGroup topButtonGroup = new ButtonGroup(); for (String buttonName : buttonNames) { JToggleButton tabButton = new JToggleButton(buttonName); // tabButton.setBorder(BorderFactory.createBevelBorder( // BevelBorder.RAISED, Color.LIGHT_GRAY, Color.DARK_GRAY)); topButtonGroup.add(tabButton); topPanel.add(tabButton); } this.add(topPanel); this.setVisible(true); this.pack(); }
Interestingly, when I tried to add a border, as was commented above, the extra distance between the buttons somehow disappeared. The result is as follows:

As much as possible, I would like to keep the overall look of the button the same, but the edges will be more rectangular so that the buttons will look more connected.