Given the following code:
(Not my actual code, just the simplest code I could knock together which repros the issue)
package com.example;
import javax.swing.Box;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class JTableExample {
JFrame f;
JTable table;
JTableExample()
{
f = new JFrame();
f.setTitle("JTable Example");
String[][] data = {
{ "Kundan Kumar Jha", "4031", "CSE" },
{ "Anand Jha", "6014", "IT" }
};
String[] columnNames = { "Name", "Roll Number", "Department" };
JPanel outerPanel = new JPanel();
outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));
for (int i = 0 ; i < 3; i++){
JPanel tablePanel = new JPanel(new BorderLayout());
table = new JTable(data, columnNames);
tablePanel.add(table.getTableHeader(), BorderLayout.PAGE_START);
tablePanel.add(table, BorderLayout.CENTER);
outerPanel.add(tablePanel);
outerPanel.add(Box.createRigidArea(new Dimension(0,20)));
}
JScrollPane sp = new JScrollPane(outerPanel);
f.add(sp);
f.setSize(500, 400);
f.setVisible(true);
}
public static void main(String[] args)
{
new JTableExample();
}
}
You get a window which looks like so:
What I want is for the tables to all be packed at the top of the window, with all the blank space at the bottom, like so:
(I bodged this screenshot by adding 20 JPanels after the tables)
I have attempted to use a GridBagLayout, but what ends up happening is this:
(Everything just floating in the middle of the window)
Can anyone help me understand what I am doing wrong?
In case it matters, in my actual code, there is an arbitrary number of tables, it’s not fixed.