How to stop BasicArrowButton from expanding to take the entire toolbar?
Asked Answered
G

2

1

I am creating an window with a JToolBar containing two buttons. One is a regular JButton and the other is a BasicArrowButton (javax.swing.plaf.basic.BasicArrowButton). Without doing any additional configuration, the JButton does not expand in the toolbar, but the BasicArrowButton expands to occupy the complete toolbar.

I have tried to configure it to fit in a small 16x16 square by setting its maximum and preferred size to 16x16. But that doesn't work. Also tried using setSize() without success. Can anyone tell me where the problem could be?

I have also tried to use a horizontal glue (I am using WindowBuilder with Eclipse) to the right of the BasicArrowButton. That didn't work either.

I am using JDK1.7.0_07.

public class ToolbarTest {

    private JFrame frame;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ToolbarTest window = new ToolbarTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ToolbarTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JToolBar toolBar = new JToolBar();
        frame.getContentPane().add(toolBar, BorderLayout.NORTH);

        JButton btnEdit = new JButton("Edit");
        toolBar.add(btnEdit);

        //------------------------------------------------------------
        JButton btnUp = new BasicArrowButton(BasicArrowButton.NORTH);
        btnUp.setSize(new Dimension(16, 16));
        btnUp.setMaximumSize(new Dimension(16, 16));
        toolBar.add(btnUp);
        //------------------------------------------------------------
    }
}

enter image description here

Gesticulation answered 21/1, 2013 at 20:27 Comment(5)
For better help sooner, post an SSCCE. It is likely you need to override the getMaximumSize() to instead return getPreferredSize().Escaut
It is not recommended to use components from the Look and Feel packages in this mannerLeyla
You shouldn't set sizes. Instead try using a layout manager: toolBar.setLayout(...);Horseshoes
@Leyla Thanks. I wasn't aware of it. I am reading about it now. I would now rather use a button with an arrow icon.Gesticulation
Try to add an empty JPanel or JLabel() as the third componentRaycher
S
2

Internally, JToolBar uses DefaultToolBarLayout, a subclass of BoxLayout. You can substitute your own using setLayout(). FlowLayout may be a suitable choice:

JToolBar bar = new JToolBar("Edit Menu");
bar.setLayout(new FlowLayout(FlowLayout.LEFT));
Smalls answered 22/1, 2013 at 10:26 Comment(0)
G
2

Yet another example why you shouldn't call any of the setXXSize methods :-)

As @trashgod already mentioned, the default LayoutManager of the JToolBar is-a BoxLayout which respects the maxSize of a component. So trying to somehow make the button return something reasonable is a step into the correct direction. Which surprisingly has no effect at all.

The reason it has no effect here is that ... BasicArrowButton returns hard-coded sizes, that is the value set via XX is simply ignored. Here's the core snippet:

public Dimension getMaximumSize() {
    return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}

The way out is to extend and override, f.i.:

JButton btnUp = new BasicArrowButton(BasicArrowButton.NORTH) {

    @Override
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }

};
Goodhumored answered 22/1, 2013 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.