I'm using a GridBagLayout Manager to define clearly where (for now) thow labels and a JScrollPane should be.
I use gridbagconstraint to tell the first JLabel named "label1" (with text) he should be 3 unit wide * 1 units tall. I tell another JLabel named "blank" (empty) he should be 9 units wide * 1 unit tall.
Then I declare a JScrollPane, and tell it it should be on y=1, height = 11, and x=0 width = 12.
I set gridbag's contraint's fill to BOTH, because want each component to fill the rectangle I describe.
label1 should be a quarter of the whole width available, but since label1 has some text, label1 takes 2/3 of the space.
Is there a way to just ignore the PreferredSizes of the components, just like a GridLayout (kind of) does ?
I've read the documentation about this, but I don't find any clue.
Some source code :
public class JPanelMain {
//gridconstraints
private GridBagConstraints gbc;
private static final int min = 1;
private static final int maxSize= 12;
private static final int forth =maxSize/4;
private static final int third =maxSize/3;
public JPanelMain(JFrame frame) {
JPanel pane = new JPanel();
pane.setLayout(new GridBagLayout());
gbc = new GridBagConstraints(
0,//gridx
0,//gridy
0,//gridwidth
0,//gridheight
1,//weightx
1,//weighty
GridBagConstraints.CENTER,//anchor
GridBagConstraints.BOTH,//fill
new Insets(0,0,0,0),//insets
0,//padx
0//pady
);
JLabel label1 = new JLabel ();
label1.setBorder(BorderFactory.createTitledBorder("Label1"));
JLabel blank = new JLabel();
blank.setBorder(BorderFactory.createTitledBorder("blank"));
label1.setOpaque(true);
gbc.gridwidth =(third);
gbc.gridheight = (min);
pane.add(label1,gbc);
label1.setText("Label1");
blank.setOpaque(true);
gbc.gridx = third;
gbc.gridwidth = maxSize -third;
pane.add(blank,gbc);
gbc.gridy = min;
gbc.gridheight = maxSize - min;
gbc.gridx= 0;
gbc.gridwidth=maxSize;
DefaultListModel<String> patients = new DefaultListModel<>();
fill(patients);
JList<String> list = new JList<>(patients);
pane.add(new JScrollPane(list),gbc);
frame.add(pane);
}
private void fill (DefaultListModel<String> model) {
/**/
}
}
the result is as described (and it is the same in height):
I've read (and I believe mostly understood) the example.
There is no such concept as "units" in GridBagLayout. A GBL uses cells.
If you have two components on the first row then you have two cells. Each cell is sized by the component in the cell.
If you then have a component on the second row you have a couple of options. The component could be displayed in the first or second column. Or you could give the component a "gridwidth" of 2, which means it will fill the width of the two columns.
You could use the "weightx" constraint. It indicates how space should be allocated to a component when extra space is available. So if one component could be .20 and the other .80. This is the best way to assign space proportionally however this is only for the "extra" spaces. Each component will originally be sized at its preferred size.
If you want truly relative sizes then you can use the Relative Layout. It allows you to specify the size of components relative to the total space available, so the preferred size can be ignored.
So you would need to create a panel using the RelativeLayout
for the first row containing the two labels and then add that panel to the panel using the GridBagLayout.