JDK-4837565 : Selection error by an empty JTable
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.1_02,1.4.0,1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2003-03-26
  • Updated: 2004-05-27
  • Resolved: 2004-05-27
Related Reports
Duplicate :  
Description

Name: gm110360			Date: 03/25/2003


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OS VERSION :
Windows NT 4.0 SP5

A DESCRIPTION OF THE PROBLEM :
In a swing application if an empty JTable (with the selection mode SINGLE_SELECTION) has the focus. And the user press return. This fires an ListSelectionEvent to the listeners,
with the following infos:
1) ListSelectionModel.isSelectionEmpty() is false and
2) getSelectedRow() of the corresponding JTable Object returns 1.
All of them are incorrect because the table is empty.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;

public class TableDemo2 extends JFrame {
    private boolean DEBUG = true;
    private JTable table;

    public TableDemo2() {
        super("TableDemo 2");

        MyTableModel myModel = new MyTableModel();
        table = new JTable(myModel);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this window.
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        // <TRINH>: add a ListSelectionListener to the selection model of the table
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
            {
              public void valueChanged(ListSelectionEvent event)
              {
                int index;

                index = table.getSelectedRow();
                if (index > -1)
                  System.out.println("Error - the table is empty (selected row: " + index + ")");
              }
            });
    }

    class MyTableModel extends AbstractTableModel {
        final String[] columnNames = {"First Name",
                                      "Last Name",
                                      "Sport",
                                      "# of Years",
                                      "Vegetarian"};
        // <TRINH>: create an empty data array instead of example data
        final Object[][] data = new Object[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];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }

            if (data[0][col] instanceof Integer
                    && !(value instanceof Integer)) {
                //With JFC/Swing 1.1 and JDK 1.2, we need to create
                //an Integer from the value; otherwise, the column
                //switches to contain Strings.  Starting with v 1.3,
                //the table automatically converts value to an Integer,
                //so you only need the code in the 'else' part of this
                //'if' block.
                //XXX: See TableEditDemo.java for a better solution!!!
                try {
                    data[row][col] = new Integer(value.toString());
                    fireTableCellUpdated(row, col);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(TableDemo2.this,
                        "The \"" + getColumnName(col)
                        + "\" column accepts only integer values.");
                }
            } else {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
    }

    public static void main(String[] args) {
        TableDemo2 frame = new TableDemo2();
        frame.pack();
        frame.setVisible(true);
    }
}


EXPECTED VERSUS ACTUAL BEHAVIOR :
JTable.getSelectedRow() returns -1.
JTable.getSelectedRow() returns 1. This is an error because the table is empty

REPRODUCIBILITY :
This bug can be reproduced always.
(Review ID: 182078) 
======================================================================

Comments
EVALUATION Not a regression. Reproducible at least as far back as 1.3.1_02. ###@###.### 2003-03-27 This has been fixed in 1.5 by 4905083. Closing as a duplicate. ###@###.### 2004-05-27
27-03-2003