Java (Swing): Influence of JList height in GridBagLayout
I want to create a dialog in Java with a list box and several buttons below it. The list ends at the same height as the buttons (about one line), and the entire dialog is about two lines high.
However, I would like the dialog to be taller (maybe 10 lines) and a JList to take up most of the space. I've played around with the parameters but for the rest of my life can't get this to work. Any ideas?
Here is my current code:
//layout
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
int y = 0;
//List
gbc.gridx = 0;
gbc.gridy = y;
gbc.weighty = 3;
gbc.weightx = 1;
gbc.gridwidth= 3;
add(new JScrollPane(_myList), gbc);
_myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Buttons
gbc.gridx = 1;
gbc.gridy = ++y;
gbc.gridwidth = 1;
gbc.weighty = 0;
add(_Save, gbc);
gbc.gridx = 2;
add(_Cancel, gbc);
0
source to share
4 answers