|
|
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
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.
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
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
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!
Read more about Core Java in JavaWorld's Core Java section.