JDK-4533778 : isSelectionEmpty() returns false in empty JTable 1.3.1, 1.4beta3
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2001-11-30
  • Updated: 2004-08-20
  • Resolved: 2004-08-20
Related Reports
Duplicate :  
Relates :  
Description

Name: ddT132432			Date: 11/30/2001


c:\Programme\jdk1.3.1\bin\java -version
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

If a GUI contains a empty JTable and the user traverse it with the tab key, the
selection model of the table (and also the table) return wrong values for the
selection. For example isSelectionEmpty() returns false, but there isn't
anything in the table.

This code also has the same behavior for 1.4beta3

Example:


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

public class DefaultListSelectionModelTest {
  // a empty JTable
  private static JTable table = new JTable( new Object[][] {},
                                            new Object[] {"test"} );

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    table.getSelectionModel().addListSelectionListener(
      new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        printStatus();
      }
    } );

    frame.getContentPane().add( new JScrollPane(table));
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    } );
    frame.pack();
    frame.setVisible(true);
    printStatus();
    test();
  }

  // produce 10 tab events if you want, but it doesn't work with 1.2.2
  private static void test() {
//    try {
//      Robot robot = new Robot();
//      for(int i = 0; i < 10; i++)
//      {
//        robot.delay(3000);
//        robot.keyPress(KeyEvent.VK_TAB);
//        robot.keyRelease(KeyEvent.VK_TAB);
//      }
//    }
//    catch (AWTException awtE) {
//      awtE.printStackTrace();
//    }
  }

  private static void printStatus() {
    ListSelectionModel model = table.getSelectionModel();
    System.out.println("SelectionModel min Index: " +  model.getMinSelectionIndex());
    System.out.println("SelectionModel max Index: " +  model.getMaxSelectionIndex());
    System.out.println("SelectionModel selection is " + ( model.isSelectionEmpty() ? "" : "not ") + "empty");
    System.out.println("Table selected rows count: " +  table.getSelectedRowCount());
    System.out.println("Table selected columns count: " + table.getSelectedColumnCount());
    System.out.println("Table selected row index: " + table.getSelectedRow());
    System.out.println("Table selected column index: " +  table.getSelectedColumn());
    System.out.println();
  }
}

----- output -------
c:\Programme\jdk1.3.1\bin\java DefaultListSelectionModelTest
SelectionModel min Index: -1
SelectionModel max Index: -1
SelectionModel selection is empty
Table selected rows count: 0
Table selected columns count: 0
Table selected row index: -1
Table selected column index: -1

SelectionModel min Index: 1
SelectionModel max Index: 1
SelectionModel selection is not empty
Table selected rows count: 1
Table selected columns count: 1
Table selected row index: 1
Table selected column index: 0

SelectionModel min Index: 2
SelectionModel max Index: 2
SelectionModel selection is not empty
Table selected rows count: 1
Table selected columns count: 1
Table selected row index: 2
Table selected column index: 0
...
(Review ID: 135337) 
======================================================================

Comments
EVALUATION Agreed, this is a bug. ###@###.### 2002-10-18 This has been fixed by 4905083. Closing as a duplicate. ###@###.### 2004-08-20
18-10-2002