Reputation: 173
I'm having a problem with vertical filling in my GridBagLayout-GUI. Here is the Code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Spielklasse {
public static void main(String[] args) {
JPanel page = new JPanel();
page.setLayout(new GridBagLayout());
for (int i = 0; i < 5; i++) {
JPanel subPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel();
label.setText("label " + i + ":");
JTextField textField = new JTextField();
textField.setPreferredSize(new Dimension(120,20));
subPanel.add(label, BorderLayout.WEST);
subPanel.add(textField, BorderLayout.EAST);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 1;
c.gridy = i;
c.weightx = 1;
c.weighty = 1;
c.insets = new Insets(10, 20, 10, 20);
c.fill = GridBagConstraints.HORIZONTAL;
page.add(subPanel, c);
}
JScrollPane scrollPane = new JScrollPane(page);
JFrame frame = new JFrame();
frame.setSize(300,500);
frame.add(scrollPane);
frame.setVisible(true);
}
}
I want to have label+textfield directly under the previous ones without filling the vertical space between (like when you resize my example vertically till you can see the vertical scrollbar). How can I achieve that? With my code the whole panel is filled vertically, but I don't want this.
Upvotes: 1
Views: 1553
Reputation: 51485
I made a few changes to your code.
Always start a Swing application with a call to the SwingUtilities invokeLater method. This puts the Swing components on the Event Dispatch thread.
Since you're using the GridBagLayout, you can lay out your labels and text fields directly on the page JPanel.
I created a GridBagConstraints for each label and text field. That way, we're not relying on defaults. We specify the correct anchor and fill values for each component.
I added a couple of missing JFrame methods. You either have to specify a default close operation, or you have to close the application yourself. Without this specification, your application will continue to run after you close the main JFrame.
Edited to add:
Here's the modified code.
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Spielklasse implements Runnable {
private static final Insets baseInsets =
new Insets(10, 20, 10, 20);
@Override
public void run() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
JPanel page = new JPanel();
page.setLayout(new GridBagLayout());
int gridy = 0;
for (int i = 0; i < 5; i++) {
JLabel label = new JLabel();
label.setText("Label " + i + ":");
addComponent(page, label, 0, gridy,
1, 1, baseInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JTextField textField = new JTextField(20);
addComponent(page, textField, 1, gridy++,
1, 1, baseInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
}
mainPanel.add(page);
JScrollPane scrollPane = new JScrollPane(mainPanel);
JFrame frame = new JFrame("Spielklasse");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight,
Insets insets, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor,
fill, insets, 0, 0);
container.add(component, gbc);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Spielklasse());
}
}
Upvotes: 1
Reputation: 10994
Remove next line c.weighty = 1;
.
Read more in docs.
Edit: use dummy JLabel
for grabbing free space, after loop:
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 5;
c.fill = GridBagConstraints.BOTH;
c.weightx=1;
c.weighty=1;
c.gridwidth = 2;
page.add(new JLabel(" "),c);
Upvotes: 4