Koko. N
Koko. N

Reputation: 47

Set the size of JButton to the length and width of the label

I am working on adding buttons to a button panel to be more familiar with java.swing class. Is there a way to set the size of JButton to the length and width of the button label?

Similar to setting height and width view in the xml file to wrap_content or match_parent.Setting a View or Layouts size based on either it's contents or the parent's dimensions rather than explicitly specifying a dimension.

android:layout_width="wrap_content"
android:layout_height="match_parent"

Is there a way to set the size of JButton to the length and width of the button label using button.setPreferredSize() without explicitly specifying the dimension?

button = new JButton[buttonName.length];
    RadioButtonAction radioButtonEventO=new RadioButtonAction(); //O for operation
    buttonPanelS=new JPanel();
    buttonPanelS.setLayout(new GridLayout(4,1));
    for(int i=0; i<buttonName.length;i++){
        button[i]=new JButton(buttonName[i]);
        button[i].setMargin(new Insets(0,0,0,0));
        button[i].addActionListener(radioButtonEventO);
        //button[i].setPreferredSize();
        buttonPanelS.add(button[i]);
    }
    operationPanel.add(buttonPanelS);

Upvotes: 0

Views: 943

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347244

I'm not 100% I understand, but GridLayout will, by design, size the all the components to occupy the available space of the container, evenly.

It sounds more like what you're after is GridBagLayout...

Something like

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
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();
                }

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

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JButton[] button = new JButton[5];
            JPanel buttonPanelS = new JPanel();
            buttonPanelS.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            for (int i = 0; i < 5; i++) {
                button[i] = new JButton(random());
                button[i].setMargin(new Insets(0, 0, 0, 0));
                buttonPanelS.add(button[i], gbc);
            }
            add(buttonPanelS);
        }

        private Random rnd = new Random();

        public String random() {
            int length = rnd.nextInt(20) + 1;
            StringBuilder sb = new StringBuilder(length);
            for (int index = 0; index < length; index++) {
                sb.append(('a' + rnd.nextInt(52)));
            }
            return sb.toString();
        }

    }

}

If that's not what you're after either, then consider providing a drawing, to better illustrate your question

Upvotes: 2

Related Questions