ryvantage
ryvantage

Reputation: 13486

Why does this component not align (MigLayout)

I am having the most frustrating time trying to get this component to align properly.

This SSCCE:

public static void main(String[] args) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Windows".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }
    JFrame frame = new JFrame();
    frame.setLayout(new MigLayout(new LC().fill()));
    final JList<String> company_list = new JList<>();
    company_list.setListData(new String[] {"Company 1", "Company 2", "Company 3", "Company 4", "Company 5"});
    JScrollPane company_list_jsp = new JScrollPane(company_list);
    frame.add(company_list_jsp, "dock west, width 150px, growx");
    final MainPanel mainPanel = new MainPanel();
    frame.add(mainPanel, "grow");
    company_list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            mainPanel.setCompanyName(company_list.getSelectedValue());
        }
    });
    frame.setSize(800,600);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

static class MainPanel extends JPanel {

    JLabel company_name_label;
    JList company_contacts_list;
    JList customers_list;
    JTextArea company_notes_textArea;
    public MainPanel() {
        setLayout(new MigLayout(new LC().fillX()));
        company_name_label = new JLabel();
        company_name_label.setText("Company");
        company_name_label.setFont(new Font("Tahoma", Font.PLAIN, 24));
        add(company_name_label, "cell 0 0 1 3, span");

        JPanel panel1 = new JPanel(new MigLayout(new LC().fill().insetsAll("0")));
        JLabel label1 = new JLabel("Company Contacts");
        panel1.add(label1, "cell 0 1 1 1, sg 1");
        JLabel label2 = new JLabel("Customers");
        panel1.add(label2, "cell 1 1 1 1, sg 1");
        JLabel label3 = new JLabel("Company Notes");
        panel1.add(label3, "cell 2 1 1 1, sg 1");

        company_contacts_list = new JList();
        JScrollPane company_contacts_list_jsp = new JScrollPane(company_contacts_list);
        panel1.add(company_contacts_list_jsp, "cell 0 2 1 1, height 150px, growx, sg 2");

        customers_list = new JList();
        JScrollPane customers_list_jsp = new JScrollPane(customers_list);
        panel1.add(customers_list_jsp, "cell 1 2 1 1, height 150px, growx, sg 2");

        company_notes_textArea = new JTextArea();
        company_notes_textArea.setColumns(20);
        company_notes_textArea.setRows(5);
        company_notes_textArea.setWrapStyleWord(true);
        company_notes_textArea.setLineWrap(true);
        JScrollPane company_notes_textArea_jsp = new JScrollPane(company_notes_textArea);
        panel1.add(company_notes_textArea_jsp, "cell 2 2 1 1, height 150px, growx, sg 2");

        add(panel1, "span, grow");
    }

    public void setCompanyName(String companyName) {
        company_name_label.setText(companyName);
    }
}

Produces this:

enter image description here

Where the JTextArea is not aligned properly with the other two components.

Note: if you comment out the Windows LookAndFeel code, it works:

enter image description here

But changing the LAF of my application is not an option.

Does anyone know why this is happening?

Upvotes: 1

Views: 202

Answers (1)

David Yee
David Yee

Reputation: 3646

You can specify that entire 3rd row to fill vertically by setting the rowConstraints parameter of the MigLayout to "[][][fill]". For example:

JPanel panel1 = new JPanel(new MigLayout("fill,insets 0", "[][][]", "[][][fill]"));

MigLayout with the third row vertically filling

Upvotes: 1

Related Questions