Search code examples
javaswinglayout-managerboxlayout

How to prevent enlarging JFormattedTextField for BoxLayout?


I would like to have JFormattedTextField components in one column. I don´t understand why this code causes to enlarge to maximum size.

    // Create the text fields and set them up.
    priemerKolesa = new JFormattedTextField(numberFormat);
    priemerOsi = new JFormattedTextField(numberFormat);
    hrubkaPlasta = new JFormattedTextField(numberFormat);
    hrubkaKolesa = new JFormattedTextField(numberFormat);
    hrubkaNavinu = new JFormattedTextField(numberFormat);
    hrubkaNavinu.setColumns(6);
    // Layout the text fields in a panel.
    JPanel fieldPane = new JPanel();
    fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.Y_AXIS));
    fieldPane.add(priemerKolesa);
    fieldPane.add(priemerOsi);
    fieldPane.add(hrubkaPlasta);
    fieldPane.add(hrubkaKolesa);
    fieldPane.add(hrubkaNavinu);

I want to have it in a nice stack, not expanded like this.


Solution

  • It seems that BoxLayout stretches components to fit the available space. One way around that it to first put each component into a JPanel with FlowLayout (which will not stretch the components within) then add that panel to the container using BoxLayout. E.G. in the form of an MCVE as mentioned above:

    import java.awt.*;
    import java.text.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    public class BoxLayoutQuandary {
    
        private JComponent ui = null;
    
        BoxLayoutQuandary() {
            initUI();
        }
    
        public void initUI() {
            if (ui != null) {
                return;
            }
    
            ui = new JPanel(new BorderLayout(4, 4));
            ui.setBorder(new EmptyBorder(4, 4, 4, 4));
    
            NumberFormat numberFormat = new DecimalFormat("##.#");
            // Create the text fields and set them up.
            JFormattedTextField priemerKolesa = new JFormattedTextField(numberFormat);
            priemerKolesa.setColumns(8);
            JFormattedTextField hrubkaNavinu = new JFormattedTextField(numberFormat);
            hrubkaNavinu.setColumns(6);
            // Layout the text fields in a panel.
            JPanel fieldPane = new JPanel();
            fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.Y_AXIS));
            ui.add(fieldPane);
            JPanel priemerKolesaPanel = new JPanel();
            priemerKolesaPanel.add(priemerKolesa);
            fieldPane.add(priemerKolesaPanel);
            JPanel hrubkaNavinuPanel = new JPanel();
            hrubkaNavinuPanel.add(hrubkaNavinu);
            fieldPane.add(hrubkaNavinuPanel);
        }
    
        public JComponent getUI() {
            return ui;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception useDefault) {
                    }
                    BoxLayoutQuandary o = new BoxLayoutQuandary();
    
                    JFrame f = new JFrame(o.getClass().getSimpleName());
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setLocationByPlatform(true);
    
                    f.setContentPane(o.getUI());
                    f.pack();
                    f.setMinimumSize(f.getSize());
    
                    f.setVisible(true);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }