Search code examples
javaswingjbuttonlayout-managerpreferredsize

Button size (Java)


I created a simple menu in Java, but I can't figure out how to change the size of a button. My menu looks like this: Current look

I want the last button to have same size like other buttons.

tlacTisk.setSize(10,10);
tlacTisk.setPreferredSize(10,10);

doesn't work.

Code, where I created buttons and box:

JButton tlacSVG = new JButton();
        tlacSVG.setText("Export do SVG");
        tlacSVG.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                exportujSVG();
            }
        });

        JButton tlacPNG = new JButton();
        tlacPNG.setText("Export do PNG");
        tlacPNG.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                exportujPNG();
            }
        });

        JButton tlacTisk = new JButton();
        tlacTisk.setText("Tisk...");
        tlacTisk.setPreferredSize(new Dimension(50, 25));
        tlacTisk.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                tiskni();
            }
        });


        Box boxTlacitek = Box.createVerticalBox();
        boxTlacitek.add(Box.createVerticalStrut(5));
        boxTlacitek.add(tlacSVG);
        boxTlacitek.add(Box.createVerticalStrut(10));
        boxTlacitek.add(tlacPNG);
        boxTlacitek.add(Box.createVerticalStrut(10));
        boxTlacitek.add(tlacTisk);
        boxTlacitek.setBorder(BorderFactory.createTitledBorder("Menu"));

        okno.add(boxTlacitek, BorderLayout.EAST);

Can you give me advice how I can change size? Thanks.


Solution

  • Different layouts managers treat preferred size differently. Also, setting size with setSize() is not a good idea. Let the layout manager do the layout for you. See A Visual Guide to Layout Managers for more details and examples.

    For example you can create a separate panel that holds the buttons. Set its layout to GridLayout. In this layout the components takes all the available space within its cell, and each cell is exactly the same size. Add this panel to the container. See How to Use GridLayout for examples.

    Here is a simple demo of GridLayout and GridBagLayout :

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    
    public class DemoButtons {
        public DemoButtons() {
            final JFrame frame = new JFrame("Demo buttons");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel buttonPanel = new JPanel(new GridLayout(3, 1));
            buttonPanel.add(new JButton("Export do SVG"));
            buttonPanel.add(new JButton("Export do PNG"));
            buttonPanel.add(new JButton("Tisk..."));
    
            JPanel east = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weighty = 1;
            east.add(buttonPanel, gbc);
    
            JPanel center = new JPanel(){
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(200, 200);
                }
            };
            center.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    
            frame.add(east, BorderLayout.EAST);
            frame.add(center);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new DemoButtons();
                }
            });
        }
    }