The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Layout  [4 examples]

e949. Packing a JTable Component

This example demonstrates how to adjust the preferred size of a JTable to be just large enough to accommodate the preferred size of all cells.
    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols) {
        // Override this method so that it returns the preferred
        // size of the JTable instead of the default fixed size
        public Dimension getPreferredScrollableViewportSize() {
            return getPreferredSize();
        }
    };
    
    // Allow columns to be resized
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    
    // Add data...
    
    // These packing methods are defined in e950 Packing a Column of a JTable Component
    // and e913 Setting the Height of a Row in a JTable Component
    packColumns(table, 2);
    packRows(table, 0);

 Related Examples
e950. Packing a Column of a JTable Component
e951. Setting Grid Line Properties in a JTable Component
e952. Setting the Gap Size Between Cells in a JTable Component

See also: Cells    Column Heads    Columns    Editing    Events    Rows    Scrolling    Selection    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.