-
Bug
-
Resolution: Cannot Reproduce
-
P4
-
None
-
1.1.6
-
x86
-
windows_95
Name: clC74495 Date: 10/06/98
A ListSelectionListener registered with the SelectionModel
in a JTable is not always notified after a call to the
SelectionModel's setSelectionInterval() method.
In the sample code (follows) one row after the other is
added to the Table's model and is selected after this.
The number of the selected line is printed to System.out.
When the ListSelectionListener is called, it will ask the
SelectionModel, what the lead selection is and prints it, too.
The output looks like this:
ListSelectionListener: Lead selection is -1
ListSelectionListener: Lead selection is 0
Row 0 selected.
ListSelectionListener: Lead selection is 0
Row 1 selected.
ListSelectionListener: Lead selection is 1
Row 2 selected.
ListSelectionListener: Lead selection is 2
Row 3 selected.
ListSelectionListener: Lead selection is 3
Row 4 selected.
ListSelectionListener: Lead selection is 4
Row 5 selected.
ListSelectionListener: Lead selection is 5
Row 6 selected.
ListSelectionListener: Lead selection is 6
Row 7 selected.
ListSelectionListener: Lead selection is 7
Row 8 selected.
ListSelectionListener: Lead selection is 8
Row 9 selected.
After the last selection, the listener is not notified,
but the table row is selected (displayed in the selection
background/foreground colors).
This is the code to produce the output above:
import com.sun.java.swing.*;
import com.sun.java.swing.table.*;
import java.util.Vector;
import com.sun.java.swing.event.*;
import java.awt.BorderLayout;
class TableTest
extends JFrame {
JTable tb;
public TableTest() {
Object col[] = { "col1" };
DefaultTableModel model = new DefaultTableModel( col, 0 );
tb = new JTable( model );
tb.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
System.out.println( "ListSelectionListener: Lead selection is "
+ String.valueOf(tb.getSelectionModel().getLeadSelectionIndex()) );
}
} );
Vector v;
int row;
for( int i = 0; i < 10; ++i ) {
v = new Vector();
v.addElement( String.valueOf( i ) );
((DefaultTableModel) tb.getModel()).addRow( v );
// index is count minus one!
row = tb.getModel().getRowCount() - 1;
tb.getSelectionModel().setSelectionInterval( row, row );
System.out.println( "Row " + String.valueOf( row ) + " selected." );
}
setLayout( new BorderLayout() );
add( new JScrollPane( tb ), "Center" );
pack();
show();
}
public static void main( String args[] ) {
new TableTest();
}
};
(Review ID: 39784)
======================================================================