JDK-4393423 : JTable.getSelectedRow incorrect with empty table, after ENTER is pressed.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2000-11-29
  • Updated: 2004-08-20
  • Resolved: 2004-08-20
Related Reports
Duplicate :  
Relates :  
Description

Name: rmT116609			Date: 11/29/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

I think this is a variant of (allegedly fixed) bug 4128505.

  To reproduce the problem:
1. compile the following test program it's based on the one provided for bug
#4128505,


import javax.swing.*;
import javax.swing.table.*;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.event.*;

class MyTable extends JTable {
  MyTable(TableModel m) {
    super(m);
  }

  public void valueChanged(ListSelectionEvent e) {
    System.out.println("-----------------");
    System.out.println("row count = " + getRowCount());
    System.out.println("selected row count = " + getSelectedRowCount());
    System.out.println("selected row = " + getSelectedRow());
  }
}

public class SimpleTableDemo extends JPanel {
    public SimpleTableDemo() {
        JTable table = new MyTable(new MyTableModel());

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = JTable.createScrollPaneForTable(table);
        scrollPane.setPreferredSize(new Dimension(400, 100));

        //Add the scroll pane to this panel.
        setLayout(new GridLayout(1, 0));
        add(scrollPane);
    	  System.out.println("row count = " + table.getRowCount());
        System.out.println("selected row count = " + table.getSelectedRowCount());
        System.out.println("selected row = " + table.getSelectedRow());
    }

    /*
     * Instead of creating this class, you COULD just put columnNames
     * and data into the SimpleTableDemo class, creating the table
     * with new JTable(data, columnNames).  However, if you tried to
     * add any functionality to the table, such as editing, then you'd
     * run into trouble.
     *
     * It's best in the long run to create your own table model.
     * (Besides, it's not difficult at all!)
     */
    class MyTableModel extends AbstractTableModel {
        final String[] columnNames = {"First Name",
                                      "Last Name",
                                      "Sport",
                                      "Est. Years Experience"};
      /*
        final String[][] data = {
            {"Mary", "Campione", "Snowboarding", "5"},
            {"Alison", "Huml", "Rowing", "3"},
            {"Kathy", "Walrath", "Chasing toddlers", "2"},
            {"Mark", "Andrews", "Speed reading", "20"},
            {"Angela", "Lih", "Teaching high school", "4"}
        };
	*/

      final String[][] data = new String[0][0];

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("SimpleTableDemo");

        frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        frame.getContentPane().add(new SimpleTableDemo(), BorderLayout.CENTER);
        frame.setSize(400, 125);
        frame.setVisible(true);
    }
}



2. run it, and mouse click on the empty JTable to give it the focus.
3. press Enter repeatedly.
 (it *incorrectly* reports that the selected row exceeds the numbr of rows in
the table.)

4. The output I get is as follows:

E:\test>java SimpleTableDemo
-----------------
row count = 0
selected row count = 0
selected row = -1
row count = 0
selected row count = 0
selected row = -1
-----------------
row count = 0
selected row count = 1
selected row = 1
-----------------
row count = 0
selected row count = 1
selected row = 2
-----------------
row count = 0
selected row count = 1
selected row = 3
-----------------
row count = 0
selected row count = 1
selected row = 4
-----------------
row count = 0
selected row count = 1
selected row = 5
-----------------
row count = 0
selected row count = 1
selected row = 6
(Review ID: 112990) 
======================================================================

Comments
EVALUATION I was able to reproduce this easily. ###@###.### 2002-10-18 This has been fixed by 4905083. Closing as a duplicate. ###@###.### 2004-08-20
18-10-2002