Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4128135

Deleting last row of table doesn't clear selection

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.2
    • 1.1.5, 1.1.6, 1.2.0
    • client-libs
    • 1.2.2
    • x86
    • windows_nt



      Name: rk38400 Date: 04/11/98


      I have a JTable, whose data model is a DefaultTableModel I've subclassed.

      I've overridden removeRow() to call super.removeRow() and then fireTableDataChanged().

      Before I add rows to the table, the selectedRow=-1 as expected.
      I first add a few rows to the table, then I delete
      the currently selected row. After I delete the last row (now rowCount=0),
      I look at the selectedRow; it returns 0 instead of -1,
      even though there no longer is a row 0.
      (Review ID: 26495)
      ======================================================================

      This is a sample that I wrote to demonstrate the problem.
      tom.santos@eng 1998-05-27

      import com.sun.java.swing.*;
      import com.sun.java.swing.table.*;
      import java.awt.event.*;

      public class Test {
          public static void main( String[] argv ) {
              JFrame frame = new JFrame( "List Test" );
              frame.addWindowListener( new WindowAdapter(){
                                       public void windowClosing( WindowEvent e ){
                                       System.exit( 0 );}});

              DefaultTableModel model = new DefaultTableModel( new Object[] { "One", "Two" }, 0 ) {
                  public void removeRow( int row ) {
                      super.removeRow( row );
                      fireTableDataChanged();
                  }
              };

              JTable table = new JTable( model );
              System.out.println( "starting selected row: " + table.getSelectedRow() );

              model.addRow( new Object[] { "smatter", "chew" } );
              model.addRow( new Object[] { "momynyms", "wentothustore" } );
              model.addRow( new Object[] { "mungry", "squeet" } );

              System.out.println( "selected row after adding items: " + table.getSelectedRow() );

              table.setRowSelectionInterval( 1, 1 );

              System.out.println( "selected row after setting selection: " + table.getSelectedRow() );

              model.removeRow( 1 );
              model.removeRow( 0 );
              model.removeRow( 0 );

              System.out.println( "selected row after deletions: " + table.getSelectedRow() );

              frame.getContentPane().add( table );

              frame.pack();
              frame.show();
          }
      }

            pmilnesunw Philip Milne (Inactive)
            rkarsunw Ralph Kar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: