I have a JScrollPane
containing a JTable
in a GridBagLayout
. When I resize my JFrame
containing the layout pane, the table resizes horizontally but not vertically. Even if I have GridBagConstraints.fill
set to GridBagConstraints.BOTH
. I want to have a JScrollPane
resizing vertically and horizontally depending on the size of its parent container (→ the JFrame
).
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class GridBagPanel extends JPanel {
private static final long serialVersionUID = 1L;
public GridBagPanel() {
super(new GridBagLayout());
String[] columns = { "Column 1", "Column 2", "Column 3", "Column 4" };
String[][] contents = { { "Content 1", "Content 2", "Content 3", "Content 4", "Content 5" },
{ "Content 1", "Content 2", "Content 3", "Content 4", "Content 5" },
{ "Content 1", "Content 2", "Content 3", "Content 4", "Content 5" },
{ "Content 1", "Content 2", "Content 3", "Content 4", "Content 5" } };
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JTable table = new JTable(contents, columns);
add(new JScrollPane(table), gbc);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 1;
JButton button = new JButton("Button");
JPanel panel = new JPanel();
panel.add(button);
add(panel, gbc);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
GridBagPanel gbp = new GridBagPanel();
frame.add(gbp);
frame.pack();
frame.setVisible(true);
}
}
The result is the following:
Is there a way to fix the height of the scroll pane's viewport to the height of its parent container so that the entire JFrame
is being filled by its components?
The alignment of the button should stick to the border of the scroll pane, not like shown in the screenshot.
Thanks in advance.
EDIT:
Thanks to Hovercraft Full Of Eels I've come up with the following fix:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;
public class LayoutTest {
public static void main(String[] args) {
String[] columns = { "Column 1", "Column 2", "Column 3", "Column 4" };
String[][] contents = { { "Content 1", "Content 2", "Content 3", "Content 4", "Content 5" },
{ "Content 1", "Content 2", "Content 3", "Content 4", "Content 5" },
{ "Content 1", "Content 2", "Content 3", "Content 4", "Content 5" },
{ "Content 1", "Content 2", "Content 3", "Content 4", "Content 5" } };
JFrame frame = new JFrame();
JPanel wrapper = new JPanel();
JPanel buttonPanel = new JPanel();
JButton button = new JButton("Button");
JTable table = new JTable(contents, columns);
JScrollPane scrollpane = new JScrollPane(table);
wrapper.setLayout(new BorderLayout());
buttonPanel.add(button);
wrapper.add(scrollpane, BorderLayout.CENTER);
wrapper.add(buttonPanel, BorderLayout.PAGE_END);
frame.add(wrapper);
frame.pack();
frame.setVisible(true);
}
}