Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 116: Set your table options -- at runtime!

Enhance the display and usability of JTable

  • Print
  • Feedback

Page 2 of 3

Figure 1. Messy scroll



As shown in Figure 1, it's difficult to discern any column headers or table data. Although we've added a scroll bar, the horizontal scroll bar does not appear. A close look at the JTable class reveals why. The JTable class has an attribute for auto-resize mode, which determines if the table automatically resizes the column widths (to cover the table's entire width) and how it does that resizing. This can take any of the following values:

  • AUTO_RESIZE_OFF: Do not adjust column widths automatically; use a scroll bar
  • AUTO_RESIZE_NEXT_COLUMN: When a column is adjusted in the UI, adjust the next column the opposite way
  • AUTO_RESIZE_SUBSEQUENT_COLUMNS: During UI adjustment, change subsequent columns to preserve the total width
  • AUTO_RESIZE_LAST_COLUMN: During all resize operations, apply adjustments to the last column only
  • AUTO_RESIZE_ALL_COLUMNS: During all resize operations, proportionately resize all columns


By default, the JTable resizes the other columns to preserve overall appearance, which explains Figure 1. Hence, if we want to display the columns with a horizontal scroll bar, we add a method to MyTable and call it from the constructors:

    /**This method shows the horizontal scroll bar when required.
     * It's being called in the two constructors provided here.
     */
     public void showHorScroll(boolean show){
      if (show){
          setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      }else{
          setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
      }
     }


Figure 2 shows the display with the visible horizontal scroll bar:

Figure 2. Visible horizontal scroll bar

Controlling JTable columns

You can control the width of your columns, as well as make them nonresizable. This section shows you how.

Wider columns

Often you want a column wider or narrower than another. To change a column's width, you use the TableColumnModel:

      /**This method should be called to set the column
        *at pColumn index to a width of pWidth.
          */
      public void setColumnWidth(int pColumn, int pWidth){
            //Get the column model.
            TableColumnModel colModel = getColumnModel();
            //Get the column at index pColumn, and set its preferred width.
                colModel.getColumn(pColumn).setPreferredWidth(pWidth);
      }


You can also add a button and its action listener to the JFrame, so that clicking the button changes the table's width:

    JButton resizeButton = new JButton("Resize Third Column");
    setResizeButton.addActionListener(this);
    public void actionPerformed(ActionEvent e){
      //Check which button was clicked.
          if (e.getActionCommand().equals("Resize Third Column")){
                 System.out.println("Resize called - resizes third column
    to 300");
                 table.setColumnWidth(2, 300);
                 //Force GUI update.
                 table.invalidate();
                 frame.invalidate();
                 frame.validate();
                 frame.repaint();
            }


In this case, pColumn is the column index, and pWidth is the new width set. The before and after of clicking the Resize button are shown in Figures 3 and 4.

Figure 3. Before clicking the Resize button

Figure 4. After clicking the Resize button

Nonresizable columns

For general use, you can resize columns by dragging the headers. The following code removes the ability to resize based on pIsResize. If pIsResize is true, the column can be resized; otherwise, it cannot be resized:

  • Print
  • Feedback

Resources