JDK-4276786 : JTable looses selection when the model fires "fireTableDataChanged"
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0,1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_8,windows_nt
  • CPU: x86,sparc
  • Submitted: 1999-09-29
  • Updated: 1999-10-15
  • Resolved: 1999-10-15
Related Reports
Duplicate :  
Description

Name: krT82822			Date: 09/29/99


Before 1.3 beta, fireTableDataChanged() kept the selected lines.
Now, the behavior is different, the whole selection is lost.

------------

9/29/99 eval1127@eng -- setNumRows() deprecated, so tried tablemodel.removeRow(), followed by fireTableDataChanged...sure enough, selection lost.
Previous bugs seemed to deal with this, but can't find one that's still open.

Using variant of test case from bug # 4267264:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class TestTable {

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

                final DefaultTableModel model = new DefaultTableModel( 0, 2 );
                model.addRow (new Object[] {"R1", "r1"});
                model.addRow (new Object[] {"R2", "r2"});
                model.addRow (new Object[] {"R3", "r3"});

                final JTable table = new JTable( model );

                JButton button = new JButton( "get selection Indexes" );
                JButton button0 = new JButton( "setNumRows( 0 )" );
                JButton button1 = new JButton( "setNumRows( 1 )" );
                button.addActionListener( new ActionListener() {
                    public void actionPerformed( ActionEvent e ) {
                        System.out.println ("anchorRow = " 
                        + table.getSelectionModel().getAnchorSelectionIndex());
                        System.out.println ("anchorCol = " 
                        + table.getColumnModel().getSelectionModel().getAnchorSelectionIndex());
                    }
                });
                button0.addActionListener( new ActionListener() {
                    public void actionPerformed( ActionEvent e ) {
                        model.setNumRows( 0 );
                    }
                });

                button1.addActionListener( new ActionListener() {
                    public void actionPerformed( ActionEvent e ) {
                        // model.setNumRows( 1 );
                                                model.removeRow(2);
                                                model.removeRow(1);
                                                // model.fireTableDataChanged();	// this makes selection get lost
                    }
                });

                frame.getContentPane().setLayout( new GridLayout( 4, 1 ) );
                frame.getContentPane().add( table );
                frame.getContentPane().add( button );
                frame.getContentPane().add( button0 );
                frame.getContentPane().add( button1 );

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

(Review ID: 95906) 
======================================================================

Comments
EVALUATION The number of rows may change with a "dataChanged" event. Since there is no way to tell which rows were added or removed the selection is no longer meaningful. Moreover, ignoring this case can produce bugs because the selected row may refer to a row that no longer exists. If the number of rows has stayed the name, and their order is the same - use tableRowsUpdated() instead, this should preserve the selection. philip.milne@eng 1999-10-15
15-10-1999