Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala swing all button same width

I have a BoxPanel of buttons in my scala swing app that looks ugly to me because the buttons are all different sizes. I had changed it to a GridPanel but then they filled the panel vertically aswell which I found uglier. How can I have all buttons fill the width of the BoxPanel but stay their perferred height?

I tried a work around, shown below, where the panel sets all the contents to the max width but it had no effect.

val buttons = new BoxPanel(Orientation.Vertical) {
  contents += new Button("Normal Button")
  contents += new Button("small")
  contents += new Button("Significantly larger button than the rest")

  val maxWidth = contents map { 
    (button: Component) => button.preferredSize 
  } maxBy { _.width }

  contents foreach { 
    (button: Component) => button.preferredSize = maxWidth
  }
}

Is there a way to make the above workaround work or a way that isn't a workaround?

like image 332
cheezsteak Avatar asked Apr 27 '26 07:04

cheezsteak


1 Answers

A s discussed in Box Layout Features, "if all the components have identical X alignment, then all components are made as wide as their container." Override the button's getMaximumSize() implementation as shown below to return an arbitrary width and the button's preferred hight. Change setHorizontalAlignment() and/or resize the frame to see the effect.

@Override
public Dimension getMaximumSize() {
    return new Dimension(
        Short.MAX_VALUE, getPreferredSize().height);
}

image

Code as shown:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/a/34443937/230513 */
public class ButtonBoxTest {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JPanel() { //arbitrary filler

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        });
        f.add(createButtonPanel(), BorderLayout.EAST);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JPanel createButtonPanel() {
        JPanel btnPanel = new JPanel();
        btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
        btnPanel.add(createButton("Button 1"));
        btnPanel.add(createButton("Button 2"));
        btnPanel.add(createButton("Long Button 3"));
        btnPanel.add(createButton("Button 4"));
        btnPanel.add(createButton("Button 5"));
        return btnPanel;
    }

    private JButton createButton(String name) {
        final JButton b = new JButton(name) {

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(
                    Short.MAX_VALUE, getPreferredSize().height);
            }
        };
        b.setAlignmentX(0.5f);
        b.setHorizontalAlignment(JButton.RIGHT);
        return b;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new ButtonBoxTest()::display);
    }
}
like image 100
trashgod Avatar answered Apr 29 '26 22:04

trashgod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!