nousicek
nousicek

Reputation: 27

JSeparator makes a long blank space

I want to put JSeparator between to sections of toolbar. The separator is visible when I add this:

toolbar.add(radioButtonDDA, BorderLayout.NORTH);
        toolbar.add(radioButtonPolygon, BorderLayout.NORTH);
        toolbar.add(new JSeparator(1));
        toolbar.add(radioButtonseedFill, BorderLayout.NORTH);
        toolbar.add(radioButtonScanLine, BorderLayout.NORTH);
        toolbar.add(patternCheckBox, BorderLayout.NORTH);
        toolbar.add(buttonClear, BorderLayout.NORTH);

However It also creates a blank space behind the separator. How to get rid of the space?

toolbar space

Upvotes: 2

Views: 663

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Instead of using a JSeparator directly, make use of JToolBars built in support

From the JavaDocs

JSeparator provides a general purpose component for implementing divider lines - most commonly used as a divider between menu items that breaks them up into logical groupings. Instead of using JSeparator directly, you can use the JMenu or JPopupMenu addSeparator method to create and add a separator.

Separator

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JToolBar toolbar = new JToolBar();
                toolbar.add(new JRadioButton("One"));
                toolbar.add(new JRadioButton("Two"));
                toolbar.addSeparator();
                //toolbar.add(new JSeparator(JSeparator.VERTICAL));
                toolbar.add(new JRadioButton("Three"));
                toolbar.add(new JRadioButton("Four"));
                toolbar.add(new JRadioButton("Five"));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(toolbar, BorderLayout.NORTH);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            add(new JLabel("Just here to fill space"));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

But why (does it do this)?

I've not delved deeply into the issue (or even shallowly), but I would surmise that the the layout manager is using the separator's maximum size property to make determinations about how best to layout the component, which is providing it with the remaining space ... this is probably why JToolBar supports it's own separator

Upvotes: 2

Related Questions