Fitting a JTable Inside a Panel

I’m using a JTable and adding it to a panel which uses a gridbaglayout like so:

JTable qdbs = new JTable(rowData, columnNamesVector);

panel.add(qdbs, c);

I don’t want the table to be in a scroll pane, but I do want the table to take up the entire width of the panel. How would I accomplish this?

An SSCCE as requested:

import javax.swing.*;
import java.awt.*;

public class Main{

    public static void main(String[] args) {
    new TestFrame();

    public static class TestFrame extends JFrame{
    public TestFrame() {

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0;

        c.insets = new Insets(10,10,10,10);
        JTable testTable = new JTable(10,2);
        panel.add(testTable, c);



I would like this table to always take up the entire width of the panel (except the insets). Currently the table does not change size when the frame is resized.


You need to add constrains to tell the layout what to do with more space. In your SSCCE add these items:

  c.fill = GridBagConstraints.BOTH;
  c.weightx = 1;
  c.weighty = 0;

Leave a Reply

Your email address will not be published. Required fields are marked *