Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 486
Sk1X1 Avatar asked Mar 28 '13 16:03

Sk1X1


People also ask

How do you change the size of a button in Java?

setMaximumSize(new Dimension(400, 400)); JButton btn; for (int i=1; i<=4; i++) { for (int j=1; j<=4; j++) { btn = new JButton(); btn. setPreferredSize(new Dimension(100, 100)); firstPanel. add(btn); } } JPanel secondPanel = new JPanel(); secondPanel. setLayout(new GridLayout(5, 13)); secondPanel.

How do you make a button smaller in Java?

Simply delete the frame. setPreferredSize() and the buttons will be small (the frame size will be determined by the preferred sizes of inner components by calling frame. pack() method).

How do I change a button in Java?

By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.

What is JButton in Java?

Introduction. The class JButton is an implementation of a push button. This component has a label and generates an event when pressed. It can also have an Image.


2 Answers

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();
            }
        });
    }
}   
like image 138
tenorsax Avatar answered Sep 18 '22 15:09

tenorsax


You should not try to control the size of your buttons (or components) directly, use the layout manager configuration of your container (the menu's panel in you case) to do so.

To do this, have a look to the differents Swing Layout Managers : Swing Layout Managers

I personnaly prefer the JGoodies form API, that I find far more simple to user and to maintains over time : JGoodies Forms API

Regards

like image 22
Marc Avatar answered Sep 17 '22 15:09

Marc