Reputation: 2425
I am using a JTabbedPane
to control a few panels, each containing fields for the user to enter information. The intention is that the user might fill them in any order, and may go back to change the information previously entered.
JTabbedPane tabs = new JTabbedPane();
tabs.add("Attributes", attributesPanel);
...
frame.add(tabs);
Each of the panels uses a GridBagLayout
along with GridBagConstraints
to format everything. When I don't enter anything and change tabs, everything seems okay. However, when I enter any information within any of the JTextField
s and change tabs, returning to the tab will show everything bunched up within the center of the frame.
Does anyone know why that may be? Again, this only occurs if I actually enter something in the text fields...
In the panels, everything is initialized in the constructor. Here is some partial code:
protected EntityBuilderAttributesPanel() {
setPreferredSize(new Dimension(EntityBuilder.PANEL_WIDTH, EntityBuilder.PANEL_HEIGHT));
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
entityIdLabel = new JLabel("Entity Unique Identity");
entityIdTextField = new JTextField();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
add(entityIdLabel, c);
c.gridx = 1;
c.gridwidth = 3;
add(entityIdTextField, c);
entityNameLabel = new JLabel("Entity Name");
entityNameTextField = new JTextField();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
add(entityNameLabel, c);
c.gridx = 1;
c.gridwidth = 3;
add(entityNameTextField, c);
space_01 = new JLabel();
space_01.setPreferredSize(new Dimension(EntityBuilder.PANEL_WIDTH, 25));
c.gridx = 0;
c.gridy = 2;
add(space_01, c);
attributesLabel = new JLabel("Entity Attributes");
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 1;
add(attributesLabel, c);
attributes = new JList(EntityAttribute.values());
attributes.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
attributes.setLayoutOrientation(JList.VERTICAL);
attributes.setVisibleRowCount(10);
JScrollPane listScroller = new JScrollPane(attributes);
listScroller.setPreferredSize(new Dimension(EntityBuilder.PANEL_WIDTH, 400));
listScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 4;
add(listScroller, c);
[...]
Upvotes: 1
Views: 131
Reputation: 324128
listScroller.setPreferredSize(new Dimension(EntityBuilder.PANEL_WIDTH, 400));
The problem is probably because you are trying to hardcode a preferred size. If a component can't be displayed at its preferred size then GridBagLayout uses the minimum size.
Instead use a method like list.setVisibleRows(...) and let the list component determine the height/width.
//entityNameTextField = new JTextField();
entityNameTextField = new JTextField(10);
Specify a number when you create the text field and again let the text field determine a reasonable size.
//space_01.setPreferredSize(new Dimension(EntityBuilder.PANEL_WIDTH, 25));
Box.createVerticalStrut(25);
Swing already provides a transparent component you can use instead of using a JLabel. The Box component will be more efficient since it doesn't use a UI like a JLabel does so there is nothing to paint.
Upvotes: 2