Layout is scaling my JList's

60 Views Asked by At

I am using the DualListBox.java example but the issue I have is that the width of the JList scales based on the length of the entry. Ideally I want the left JList to take up 40% of the width, the JButtons to take up 10% and the right JList to take up the remaining 40%.

private void initScreen()
{
    setBorder(BorderFactory.createEtchedBorder());
    setLayout(new GridBagLayout());
    title = new JTextField("");
    title.setEditable(false);
    add(title, new GridBagConstraints(0, 0, 3, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            EMPTY_INSETS, 0, 0));

    sourceLabel = new JLabel(DEFAULT_SOURCE_CHOICE_LABEL);
    sourceListModel = new SortedListModel();

    sourceList = new JList(sourceListModel);
    sourceList.addListSelectionListener(new AddSelectionListener());
    add(sourceLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
            EMPTY_INSETS, 0, 0));
    add(new JScrollPane(sourceList), new GridBagConstraints(0, 2, 1, 5, .5, 1, GridBagConstraints.CENTER,
            GridBagConstraints.BOTH, EMPTY_INSETS, 0, 0));

    addButton = new JButton(ADD_BUTTON_LABEL);
    add(addButton, new GridBagConstraints(1, 3, 1, 2, 0, .25, GridBagConstraints.CENTER, GridBagConstraints.NONE,
            EMPTY_INSETS, 0, 0));
    addButton.addActionListener(new AddListener());
    removeButton = new JButton(REMOVE_BUTTON_LABEL);
    add(removeButton, new GridBagConstraints(1, 5, 1, 2, 0, .25, GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(0, 5, 0, 5), 0, 0));
    removeButton.addActionListener(new RemoveListener());

    destLabel = new JLabel(DEFAULT_DEST_CHOICE_LABEL);
    destListModel = new SortedListModel();

    destList = new JList(destListModel);
    destList.addListSelectionListener(new RemoveSelectionListener());
    add(destLabel, new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
            EMPTY_INSETS, 0, 0));
    add(new JScrollPane(destList), new GridBagConstraints(2, 2, 1, 5, .5, 1.0, GridBagConstraints.CENTER,
            GridBagConstraints.BOTH, EMPTY_INSETS, 0, 0));
}

The DualListBBox() is added to a JPanel which is is added to a compact grid

SpringUtilities.makeCompactGrid(content, 1, 1, 6, 6, 6, 6);

I don't have if the panels have horizontal scrolling, i just want them both to be the same width. Can anyone more familiar with grids and layouts give me some pointers or do you see anything wrong in the above code for values?

Thanks!

0

There are 0 best solutions below