Name: jk109818 Date: 04/19/2002
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
A JTable will incorrectly report the selected row count
when its table model is empty. This is related to bugid#
4247579. I can see how an easy workaround will be to check
to see if the table model is empty (as suggested in bug
4247579). But I think doing so should not be necessary. I
believe that I am correct to assume that if no rows exist
in the model, calling table.getSelectedRowCount() should
ALWAYS return 0. That's just simple logic in my mind.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the attached source code.
(A frame will appear with a textbox with the initial focus
at the top and a table at the bottom)
2. Tab into the table
3. Press Tab again, and you'll notice that "1 row(s)
selected! : firstIndex=0 lastIndex=1" is printed to stdout.
(The value before "row(s)" is the value returned by
table.getSelectedRowCount().)
4. Keep pressing tab, and you'll see that 1 row will
continue to be selected (although the index changes) even
though NO rows are selected.
5. Now, uncomment the 2 lines that assign the table a model.
6. Re-run the application
7. Results are as expected.
EXPECTED VERSUS ACTUAL BEHAVIOR :
I would expect that table.getSelectedRowCount() would
return 0 when the tableModel is empty.
Right now, table.getSelectedRowCount() is returning 1.
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class JTableTabTest extends JFrame
{
public JTableTabTest()
{
super();
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 1;
constraints.insets = new Insets(3, 3, 3, 3);
getContentPane().add(new JTextField(), constraints);
final JTable table = new JTable();
table.getSelectionModel().addListSelectionListener(new
ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
int numSelectedRows = table.getSelectedRowCount
();
int firstIndex = e.getFirstIndex();
int lastIndex = e.getLastIndex();
System.out.println(numSelectedRows + " row(s)
selected! : firstIndex=" + firstIndex + " lastIndex=" + lastIndex);
}
});
// DefaultTableModel model = new DefaultTableModel(2, 2);
// table.setModel(model);
constraints.fill = GridBagConstraints.BOTH;
constraints.gridy = 1;
constraints.weightx = 1;
constraints.weighty = 1;
getContentPane().add(table, constraints);
}
public static void main(String[] arguments)
{
JTableTabTest tabTest = new JTableTabTest();
tabTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabTest.setSize(400, 400);
tabTest.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
Check to see if the model is not empty before performing
any operations on it.
(Review ID: 145295)
======================================================================