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 3 of 3

    public void setResizable(int pColumn, boolean pIsResize){
      //Get the column model.
      TableColumnModel colModel = getColumnModel();
      //Set resizable or not.
      colModel.getColumn(pColumn).setResizable(pIsResize);
    }


In this case, pColumn is the index of the nonresizable column. Getting the column (getColumn(..)) and setting a simple property (setResizable(..)) is all you need to do.

Column selections



Why not select an entire column with the click of a button rather than a single cell? The JTable displays selected/deselected cells by calling a cell's isCellSelected(int row, int col) method. Overriding this method gives you the desired results, which are dependent on the boolean select, passed as a parameter to the setSelect() method. If true, the column will be selected; if false, it will not be selected. The key is to save the column as colSelect(), with a "select" flag indicating whether this column should be selected or deselected:

    int colSelect;
    boolean select;
    /** Sets the column at index col to selected or deselected
      * -based on the value of select.
     */
     public void setSelect(int col, boolean select){
      colSelect = col;
      this.select = select;
     }
    /**This method returns whether a particular cell is selected or not.
     */
    public boolean isCellSelected(int row, int column)
      throws IllegalArgumentException{
      //override the method for the column set in setSelect()
      if (colSelect == column){
          if (select)
            return true;
          else
            return false;
      } else {
          return super.isCellSelected(row, column);
      }
    }


Figure 5 displays the result where Column D has been selected.

Figure 5. Selected column

Control headers

As you might have noticed, the column header in the first column is longer than that column's width. We address this by resetting the column width:

/**Sets the header and column size as per the Header text
*/
public void setHeaderSize(int pColumn){
      //Get the column name of the given column.
      String value =  getColumnName(pColumn);
      //Calculate the width required for the column.
      FontMetrics metrics = getGraphics().getFontMetrics();
      int width = metrics.stringWidth(value) + 
(2*getColumnModel().getColumnMargin());
      //Set the width.
      setColumnWidth(pColumn, width);
}


With the above code executed, Figure 6 shows the result of the resized column header.

Figure 6. Visible column header

A feature-rich JTable

In this tip, we've tried various display options on a simple JTable, and changed those options after the table was displayed. In the process, we developed a table that offers richer user interaction capabilities. Explore the rest of JTable's features and find out which interesting ones you can create!

About the author

Sonal Goyal has been working with Java for the past three years. She is an India-based engineer and has worked extensively on design and implementation of object-oriented systems using Java IO, JFC, CORBA, i18n, and reflection. John D. Mitchell is the Java Tips coordinator for JavaWorld.

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources