0

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:

Result

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);
    }
}
Luqus
  • 109
  • 1
  • 11
  • An obvious suggestion is to set the grid bag constraints weighty property to 1.0 or some other non-zero number, e.g., `gbc.weighty = 1;`, but I assume that you've tried this already. – Hovercraft Full Of Eels Oct 10 '21 at 12:44
  • It works fine if I set weightx and weighty to 1, but if I have another `JButton` underneath in the `GridBagLayout`, then it sticks to the actual lower border of the scrollpane when resizing, which doesn`t really exist anymore. – Luqus Oct 10 '21 at 12:52
  • Where is this information in your question? Put the JButton in its own JPanel, add that JPanel to the same container using appropriate grid bag constraints. – Hovercraft Full Of Eels Oct 10 '21 at 12:53
  • I didn't initially think this would change anything about the solution, but I'm happy to include it in my post now. – Luqus Oct 10 '21 at 12:59
  • You probably don't need to update the question at this time but rather just add the JButton and its container using a gbc.gridy = 1 – Hovercraft Full Of Eels Oct 10 '21 at 13:01
  • That is still not really working. You can see in the edited post. – Luqus Oct 10 '21 at 13:05
  • 2
    OK, nest JPanels. Outer panel should use BorderLayout. Place the gridbaglayout using JPanel into its BorderLayout.CENTER position (or skip the GBL and just add the JScrollPane there). Add the JButton-containing JPanel to the BorderLayout-using outer JPanel to the `BorderLayout.PAGE_END` position. The key is to continually experiment to find the best solution. – Hovercraft Full Of Eels Oct 10 '21 at 13:07
  • Or use different weighty values for the upper and lower component to allow for different amounts of stretching of the components. Shoot, make the lower JPanel's weighty = 0.0 and it won't expand vertically. – Hovercraft Full Of Eels Oct 10 '21 at 13:24
  • Thank you a lot! I added the fix to the post. – Luqus Oct 10 '21 at 16:50

0 Answers0