Java Reference
In-Depth Information
6.
7.
public class StaticTableFrame extends SimpleFrame
8.
{
9.
10.
private String[][] entries = {{"Cell 0.0","Cell 0.1","Cell 0.2"},
11.
{"Cell 1.0","Cell 1.1","Cell 1.2"}};
12.
private String[] columnNames = {"Column 0","Column 1","Column 2"};
13.
14.
15.
public StaticTableFrame()
16.
{
17.
JTable table = new JTable(entries,columnNames);
18.
JScrollPane scrollpane = new JScrollPane(table);
19.
this .getContentPane().add(scrollpane);
}
20.
21.
22.
public static void main(String[] args)
{
23.
StaticTableFrame STF = new StaticTableFrame();
24.
STF.showIt("Static Table Frame");
25.
}
26.
}
27.
16.3.2
Table models
The method of explicitly passing the table data in the constructor is not suitable
when using tables with complex content or very large tables. So-called table models
provide an elegant method in this situation. Changing the table content at runtime
is also easy when using table models. Table models implicitly describe the content
to be rendered into a cell. The whole table does not have to exist in the form of a
two-dimensional array. The table model has to be able to provide the content of
every cell on demand. The following application is an example of this.
We construct a multiplication table. The cell in row r and column c displays
the product r
c of the row and column number. As rows and columns are indexed
beginning with zero, the first row and column consist entirely of zeros. The model
then just specifies the following rule: 'The content of cell ( r
·
c '. The model
does not have to construct a two-dimensional array with these values 1 .
We derive our own table model from the class AbstractTableModel which is
defined in the library javax.swing.table . Class AbstractTableModel requires
the implementation of the following abstract methods:
,
c )is r
·
int getRowCount()
int getColumnCount()
Object getValueAt( int r, int c)
1
The current implementation of the Java runtime system seems to explicitly construct the
table as a two-dimensional array.
Search WWH ::




Custom Search