Search code examples
javaswingcomboboxflowlayout

SetAlignment method doesn't work in FlowLayout with combobox


I'm trying to figure out how to change the alignment of a FlowLayout with a combobox, but for some reason the setAlignment method doesn't work for me; the code I have doesn't do anything. It works when I make a completely new object, for example:

buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT);

but then the HGap and VGap parts don't work correctly; instead of creating space between the components, it just expands the panel horizontally and vertically respectively. Any help is greatly appreciated!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class HW3LayoutSettings extends JFrame {
private JPanel buttonPanel = new JPanel();
private JPanel propertiesPanel = new JPanel();
private JButton comp0 = new JButton("Component 0");
private JButton comp1 = new JButton("Component 1");
private JButton comp2 = new JButton("Component 2");
private JButton comp3 = new JButton("Component 3");
private JButton comp4 = new JButton("Component 4");
private JButton comp5 = new JButton("Component 5");
private JButton comp6 = new JButton("Component 6");
private JButton comp7 = new JButton("Component 7");
private JButton comp8 = new JButton("Component 8");
private JButton comp9 = new JButton("Component 9");
private JButton comp10 = new JButton("Component 10");
private JButton comp11 = new JButton("Component 11");
private JButton comp12 = new JButton("Component 12");
private JButton comp13 = new JButton("Component 13");
private JButton comp14 = new JButton("Component 14");
private FlowLayout flow = new FlowLayout();
private JLabel alignLabel = new JLabel("Alignment");
private JLabel hGapLabel = new JLabel("HGap:");
private JLabel vGapLabel = new JLabel("VGap:");
private JTextField hGapField = new JTextField();
private JTextField vGapField = new JTextField();
private GridLayout grid = new GridLayout(3, 3);
private String[] align = {"LEFT", "CENTER", "RIGHT"};
private JComboBox combobox = new JComboBox(align);
private Integer hGapInt;
private Integer vGapInt;

public HW3LayoutSettings() {
    buttonPanel.setLayout(flow);
    buttonPanel.add(comp0);
    buttonPanel.add(comp1);
    buttonPanel.add(comp2);
    buttonPanel.add(comp3);
    buttonPanel.add(comp4);
    buttonPanel.add(comp5);
    buttonPanel.add(comp6);
    buttonPanel.add(comp7);
    buttonPanel.add(comp8);
    buttonPanel.add(comp9);
    buttonPanel.add(comp10);
    buttonPanel.add(comp11);
    buttonPanel.add(comp12);
    buttonPanel.add(comp13);
    buttonPanel.add(comp14);
    propertiesPanel.setLayout(grid);
    propertiesPanel.add(alignLabel);
    propertiesPanel.add(combobox);
    propertiesPanel.add(hGapLabel);
    propertiesPanel.add(hGapField);
    propertiesPanel.add(vGapLabel);
    propertiesPanel.add(vGapField);

    add(buttonPanel, BorderLayout.CENTER);
    add(propertiesPanel, BorderLayout.SOUTH);

    TitledBorder titled1 = new TitledBorder("Container of FlowLayout");
    buttonPanel.setBorder(titled1);
    TitledBorder titled2 = new TitledBorder("FlowLayout Properties");
    propertiesPanel.setBorder(titled2);

    combobox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String s = (String) combobox.getSelectedItem();
            switch (s) {
                case "LEFT": {
                    flow.setAlignment(FlowLayout.LEFT);
                    validate();
                    break;
                }
                case "CENTER": {
                    flow.setAlignment(FlowLayout.CENTER);
                    validate();
                    break;
                }
                case "RIGHT": {
                    flow.setAlignment(FlowLayout.RIGHT);
                    validate();
                    break;
                }
        }
        }
    });

    hGapField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            hGapInt = Integer.parseInt(hGapField.getText());
            flow.setHgap(hGapInt);
            setSize((int) (getWidth() + hGapInt), getHeight());
            validate();
        }
    });

    vGapField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            vGapInt = Integer.parseInt(vGapField.getText());
            flow.setVgap(vGapInt);
            setSize(getWidth(), (int) (getHeight() + vGapInt));
            validate();
        }
    });
}

public static void main(String[] args) {
    HW3LayoutSettings frame = new HW3LayoutSettings();
    frame.setTitle("HW3LayoutSettings");
    frame.setSize(400, 320);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}
}

Solution

  • validate();
    

    Don't use validate() when you change the layout properties.

    Instead you should use:

    buttonPanel.revalidate(); // invokes the layout manager
    buttonPanel.repaint();  // repaints components