Reputation: 273
Ok, so here's the setup: 3 components in a panel (a JScrollpane, a JPanel, and a JTabbedPane).
this.plan = new JPanel();
this.plan.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//The ScrollPane
this.SrsDocument = new JTextArea();
this.SrsDocument.setEditable(false);
this.SrsDocumentScroll = new JScrollPane(this.SrsDocument);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.33;
c.weighty = 1.0;
c.gridx = 0;
this.plan.add(this.SrsDocumentScroll, c);
...
//The Panel
this.TreePanel = new JPanel();
this.TreePanel.setLayout(new BoxLayout(this.TreePanel, BoxLayout.Y_AXIS));
this.Tree = new JTree();
((DefaultTreeModel)(this.Tree.getModel())).setRoot(null);
this.Tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.TreeScroll = new JScrollPane(this.Tree);
this.TreePanel.add(this.TreeScroll);
c.gridx = 1;
c.weightx = 0.33;
this.plan.add(this.TreePanel, c);
...
//The TabbedPane
this.currentFunction = new JTabbedPane();
c.gridx = 2;
c.weightx = 0.33;
this.plan.add(this.currentFunction, c);
I had expected this layout to have 3 columns, all with even width. yet the initial display has this.SrsDocumentScroll much narrower in width then the other 2. Additionally, as I resize the window this.SrsDocumentScroll is consumed while the other do not resize till this.SrsDocumentScroll is completely consumed. I had expected all three to resize equally as this.plan resizes. Does weightx not determine how to distribute space when resizing as I had assumed?
I should also add that the contents of the three are: textArea is filled with text, tree as just a root, and tabbed pane has just one tab. The contents of all change dynamically, but that resizing is not the behavior im having troubles with, just the resizing of the window and it consumes (as window shrinks) leftmost->rightmost instead of equally.
EDIT: This is ment to be more of a test, thats why I used 0.33 for my weightx's. the final result is ment to weight more heavily on the tabbedpane then the others. more like (0.25,0.25,0.5) but more manageable then just those values if you folo me.
Upvotes: 2
Views: 3175
Reputation: 285405
I had expected this layout to have 3 columns, all with even width. ... I had expected all three to resize equally ...
Much will likely depend on the preferredSize, maximumSize and minimumSize of the components added to the GridBagLayout. If you want equal sized components in 3 columns, don't use a GridBagLayout but rather a GridLayout(1, 3)
(for 1 row, 3 columns) or GridLayout(0, 3)
(for 3 columns and variable number of rows).
To show you what I mean, run the code below, commenting or uncommenting these two lines:
// setSizes(btn, scrollpane);
// setSizes(label, scrollpane);
GridBagTest.java
import java.awt.*;
import javax.swing.*;
public class GridBagTest {
public static void main(String[] args) {
JPanel panel = new JPanel(new GridBagLayout());
JTextArea textarea = new JTextArea(5, 30);
JScrollPane scrollpane = new JScrollPane(textarea);
JButton btn = new JButton("Foo");
JLabel label = new JLabel("Bar");
label.setBorder(BorderFactory.createTitledBorder("Bar"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
panel.add(scrollpane, gbc);
gbc.gridx = 1;
panel.add(btn, gbc);
gbc.gridx = 2;
panel.add(label, gbc);
// **** comment or uncomment the lines below
// setSizes(btn, scrollpane);
// setSizes(label, scrollpane);
JFrame frame = new JFrame("GBC Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void setSizes(Component changeComponent,
Component templateComponent) {
changeComponent.setPreferredSize(templateComponent.getPreferredSize());
changeComponent.setMaximumSize(templateComponent.getMaximumSize());
changeComponent.setMinimumSize(templateComponent.getMinimumSize());
}
}
Upvotes: 2