Java Reference
In-Depth Information
table.setRowSelectionInterval(0, 0);
ListSelectionListener lsl;
lsl = new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent lse)
{
sc.setPhoto(iiPhotos[table.getSelectedRow()]);
}
};
table.getSelectionModel().addListSelectionListener(lsl);
Every JTable instanceobtainsitsdatafromatablemodel,whichisaninstanceof
aclassthatimplementsthe javax.swing.table.TableModel interface.Ifindit
convenient to subclass the javax.swing.table.AbstractTableModel class,
which implements many of TableModel 's methods.
AbstractTableModel doesn't implement int getColumnCount() (the
numberofcolumnsinthetable), int getRowCount() (thenumberofrowsinthe
table),and Object getValueAt(int row, int col) (thevalueatthespeci-
fiedrowandcolumn),andsoitfallstothetablemodelimplementationtooverridethese
methods to return suitable values.
Although AbstractTableModel implements String getColum-
nName(int column) , this implementation only returns default names for the
columnsusingspreadsheetconventions:A,B,C,...Z,AA,AB,andsoon.Toreturna
meaningful name, this method must also be overridden.
The table component will invoke these methods as necessary. When it does, any
passed column and/or row values are relative to 0.
After creating the model, it's passed to JTable 's JTable(TableModel dm)
constructor, which creates the table component. Along with the specified table model,
theconstructorinstallsadefaultcolumnmodel(foruseinselecting,adding,removing,
andperformingotheroperationsoncolumns)andadefaultlistselectionmodel(foruse
in selecting one or more rows).
JTable(TableModel) 's default list selection model lets the user select one or
morerows.Becausetheusershouldonlybeabletoselectonerowatatime(howwould
the application display multiple planet images simultaneously?), JTable 's void
setSelectionMode(int selectionMode) method isinvoked with argument
Search WWH ::




Custom Search