19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 40.8 shows the properties, construc<strong>to</strong>rs, and methods in<br />

TableColumn for manipulating column width and specifying the cell<br />

renderer, cell edi<strong>to</strong>r, and header renderer.<br />

javax.swing.table.TableColumn<br />

#cellEdi<strong>to</strong>r: TableCellEdi<strong>to</strong>r<br />

#cellRenderer: TableCellRenderer<br />

#headerRenderer: TableCellRenderer<br />

#headerValue: Object<br />

#identifier: Object<br />

#maxWidth: int<br />

#minWidth: int<br />

#modelIndex: int<br />

#preferredWidth: int<br />

#resizable: boolean<br />

#width: int<br />

+TableColumn()<br />

+TableColumn(modelIndex: int)<br />

+TableColumn(modelIndex: int, width: int)<br />

+TableColumn(modelIndex: int, width: int,<br />

cellRenderer: TableCellRendere)<br />

+sizeWidthToFit(): void<br />

<strong>Java</strong>Beans properties with get and set<br />

methods omitted in the UML diagram<br />

The edi<strong>to</strong>r for editing a cell in this column.<br />

The renderer for displaying a cell in this column.<br />

The renderer for displaying the header of this column.<br />

The header value of this column.<br />

The identifier for this column.<br />

The maximum width of this column.<br />

The minimum width of this column (default: 15 pixels).<br />

The index of the column in the table model (default: 0).<br />

The preferred width of this column (default: 75 pixels).<br />

Indicates whether this column can be resized (default: true).<br />

Specifies the width of this column (default: 75 pixels).<br />

Constructs a default table column.<br />

Constructs a table column for the specified column.<br />

Constructs a table column with the specified column and width.<br />

Constructs a table column with the specified column, width, and<br />

cell renderer.<br />

Resizes the column <strong>to</strong> fit the width of its header cell.<br />

Figure 40.8<br />

The TableColumn class models a single column.<br />

Listing 40.4 gives an example that demonstrates table column<br />

models. The example obtains the table column model from the table<br />

(line 21), moves the first column <strong>to</strong> the second (line 22), and<br />

removes the last column (lines 23). Figure 40.9 shows the output<br />

of the program<br />

Listing 40.4 TestTableColumnModel.java<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

1 import javax.swing.*;<br />

2 import javax.swing.table.*;<br />

3<br />

4 public class TestTableColumnModel extends JApplet {<br />

5 // Create table column names<br />

6 private String[] columnNames =<br />

7 {"Country", "Capital", "Population in Millions", "Democracy"};<br />

8<br />

9 // Create table data<br />

10 private Object[][] data = {<br />

11 {"USA", "Washing<strong>to</strong>n DC", 280, true},<br />

12 {"Canada", "Ottawa", 32, true}<br />

13 };<br />

11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!