How to create ButtonGroup with connected buttons in Java?

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

Formatter preferences of eclipse

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); // Questionable line c.gridx = tabCount; c.gridy = 0; topPanel.add(tabButton, c); tabCount++; } this.add(topPanel); this.setVisible(true); this.pack(); } public static void main(String[] args) { new Exercise(); } } 

The result is as follows:

Result without spacing

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:

Result without setting negative insets

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:

With boxlayout

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.

+7
java layout swing jtogglebutton gridbaglayout
source share
2 answers

I seem to have made an embarrassing mistake. I tried to suggest dic19 to use Seaglass' and along with JTabbedPane, and I got almost what I wanted:

Tabbed Pane with Seaglass

But then I realized that the default for JTabbedPane looks exactly what I wanted:

Default look and feel

The code I use is similar to that used in Oracle's JTabbedPane tutorial :

 public class SeaGlassExercise { public static void initWindow() { JFrame frame = new JFrame("Application Name"); CustomTabbedPane content = new CustomTabbedPane(); frame.setContentPane(content); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLocationByPlatform(true); // try { // UIManager.setLookAndFeel( // "com.seaglasslookandfeel.SeaGlassLookAndFeel"); // } catch (Exception e) { // e.printStackTrace(); // } // SwingUtilities.updateComponentTreeUI(frame); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initWindow(); } }); } } class CustomTabbedPane extends JPanel { public CustomTabbedPane() { super(new GridLayout(1, 1)); JTabbedPane tabbedPane = new JTabbedPane(); JComponent panel1 = makeTextPanel("Panel #1"); tabbedPane.addTab("AAA", panel1); JComponent panel2 = makeTextPanel("Panel #2"); tabbedPane.addTab("BBB", panel2); JComponent panel3 = makeTextPanel("Panel #3"); tabbedPane.addTab("CCC", panel3); JComponent panel4 = makeTextPanel("Panel #4"); tabbedPane.addTab("DDD", panel4); add(tabbedPane); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); } protected JComponent makeTextPanel(String text) { JPanel panel = new JPanel(); JLabel filler = new JLabel(text); filler.setHorizontalAlignment(JLabel.CENTER); panel.setLayout(new GridLayout(1, 1)); panel.add(filler); return panel; } } 

Although I knew that Java Swing, by default, platform-specific appearance and perception are different on different platforms, I never thought that JTabbedPane would look as strong as shown in the tutorial:

Oracle's image for JTabbedPane

+4
source share

You can use a layout like BoxLayout to fill a gap. GridBagLayout is not the only layout. Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

You can also call JButton functions, such as setBorder () and setBackground (), to achieve the effects mentioned. As always, the API is your best friend: http://docs.oracle.com/javase/7/docs/api/

+5
source share

All Articles