Initial JDialog - JTable size

(From a recent email.) Help, I have a 13-column JTable, inside of a jscroll pane, inside of a borderlayout, inside a JDialog. Ideally, I would like the dialog to initially open up to the correct width needed to display the 13 columns without horizontal scrolling.

Setting the preferred width of each column (only) does not work very well. Setting the minWidth gets the columns to open up to the correct width, but then the last 7 or so columns fall off the right hand side of the dialog.

Any thoughts on how to get the dialog to open to the correct width, so it displays all 13 columns w/o horizontal scrolling?

A JTable JDialog size example

The answer lies not only in setting the minWidth on each column in the JTable, but also in setting the setPreferredScrollableViewportSize() of the JTable itself.

Here is the code for example JDialog (including a JTable example) that I created to test/verify that this worked:

package test.paper;

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

public class PaperDialog1 extends JDialog
{
  JPanel panel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JScrollPane theScrollPane = new JScrollPane();
  String[] columnNames = {"Column 1",
                          "Col 2",
                          "Col 3",
                          "Col 4",
                          "Some Description",
                          "Column 6",
                          "Col 7",
                          "ABS",
                          "DEF",
                          "Column xx",
                          "Override",
                          "Something",
                          "Last Col"};
  String[][] data = {
      {"Al", "Alexander","Stuff", "ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC"},
      {"Al", "Alexander","Stuff", "ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC"},
      {"Al", "Alexander","Stuff", "ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC"},
      {"Al", "Alexander","Stuff", "ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC"},
    };
  JTable paperTable = new JTable(data,columnNames);

  public PaperDialog1(Frame frame, String title, boolean modal)
  {
    super(frame, title, modal);
    try
    {
      jbInit();
      pack();
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
  }

  public PaperDialog1()
  {
    this(null, "", false);
  }
  void jbInit() throws Exception
  {
    panel1.setLayout(borderLayout1);
    getContentPane().add(panel1);
    panel1.add(theScrollPane, BorderLayout.CENTER);
    setupInitialColumnWidths();
    theScrollPane.getViewport().add(paperTable, null);
    pack();
  }

  private void setupInitialColumnWidths()
  {
    paperTable.getColumnModel().getColumn(0).setMinWidth(60);
    paperTable.getColumnModel().getColumn(1).setMinWidth(80);
    paperTable.getColumnModel().getColumn(2).setMinWidth(80);
    paperTable.getColumnModel().getColumn(3).setMinWidth(100);
    paperTable.getColumnModel().getColumn(4).setMinWidth(60);
    paperTable.getColumnModel().getColumn(5).setMinWidth(60);
    paperTable.getColumnModel().getColumn(6).setMinWidth(60);
    paperTable.getColumnModel().getColumn(7).setMinWidth(60);
    paperTable.getColumnModel().getColumn(8).setMinWidth(60);
    paperTable.getColumnModel().getColumn(9).setMinWidth(60);
    paperTable.getColumnModel().getColumn(10).setMinWidth(100);
    paperTable.getColumnModel().getColumn(11).setMinWidth(60);
    paperTable.getColumnModel().getColumn(12).setMinWidth(60);

    //paperTable.setPreferredSize(new Dimension(900,400));
    paperTable.setPreferredScrollableViewportSize(new Dimension(900,400));
  }

}

 

For me, setting the minWidth of each column in the JTable was obvious, but setting the setPreferredScrollableViewportSize() was not. Once I learned of this, the dialog properly opened to the desired size.

Oops, one other very important matter. The width param of the setPreferredScrollableViewportSize call should be at least equal to the sum of the widths of the individual columns. The sum for my 13 columns was 900, so I used that value in the setPreferredScrollableViewportSize() call.