How to stop BasicArrowButton from expanding to tak

2019-08-08 19:19发布

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

2条回答
手持菜刀,她持情操
2楼-- · 2019-08-08 19:53

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();
    }

};
查看更多
仙女界的扛把子
3楼-- · 2019-08-08 20:14

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));
查看更多
登录 后发表回答