Question

I have a JPanel which uses the FlowLayout, and a Box which has components arranged vertically. What I want, is to set the same width size of the other components to button "Remove Column". I've tried to change the size with line

removeColumnButton.setPreferredSize(new Dimension(130, 25));

but I can only change size of the height, not width.

Below is screenshot of the panel and code:

enter image description here

    JPanel eastPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
    Box eastPanelBox = Box.createVerticalBox();
        addNewColumnButton = new JButton("Add New Column");
        addNewColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(addNewColumnButton);
        eastPanelBox.add(Box.createVerticalStrut(5));

        removeColumnButton = new JButton("Remove Column"); 
        removeColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
        removeColumnButton.setPreferredSize(new Dimension(130, 25));
        eastPanelBox.add(removeColumnButton);
        eastPanelBox.add(Box.createVerticalStrut(5));

        columnField = new JTextField();
        columnField.setAlignmentX(Box.CENTER_ALIGNMENT);
        columnField.setPreferredSize(new Dimension(130, 25));
        eastPanelBox.add(columnField);
        eastPanelBox.add(Box.createVerticalStrut(5));

        columnListCB = new JComboBox(cBoxModel);
        columnListCB.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(columnListCB); 
        eastPanelBox.add(Box.createVerticalStrut(5));

        calculateColumnButton = new JButton("Calculate Column");
        calculateColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(calculateColumnButton);
    eastPanel.add(eastPanelBox);
Was it helpful?

Solution

Use a GridLayout for the container holding the column of components. Initialize it with

int vGap = 5;
new GridLayout(0, 1, 0, vGap)

which stands for 1 column, variable number of rows. The vGap parameter must be an int that represents the vertical gap between components.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top