JDK-6263446 : REGRESSION: JDK 1.5.0_x table cell editor behavior has changed
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-04-29
  • Updated: 2011-02-16
  • Resolved: 2006-04-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u8Fixed 6 b82Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08) Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]


A DESCRIPTION OF THE PROBLEM :
I am trying to achieve Excel editing behavior in a JTable. I had everything working in JDK1.4 and then it broke when going to JDK1.5. Apparently, the JDK has changed slightly. The particular Excel behavior that is broken is when double clicking a cell to start editing. The cell editor is a JTextField. In JDK1.4, double clicking the cell would start the edit process causing my JTextField cell editor to get shown and the JDK would forward the MouseEvent with a click count of 1 to the JTextField. The JTextField would respond to the MouseEvent with click count of 1 and just position the text cursor as desired. This is just like Excel.
Now in JDK1.5 with no other code changes, the double click starts the edit process and the MouseEvent is forwarded to the JTextField BUT this time with a click count of 2. The JTextField responds to this MouseEvent with click count of 2 by highlighting whatever text is in the JTextField. I don't want this initial highlight to take place.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run included test program with JDK1.5. Just double click any cell over the text inside that cell.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect for the edit process to start, the cell editor to be added to the JTable for that cell and the text cursor to be positioned where the doble click took place with no text selection/highlighting.
ACTUAL -
Everything occurs correctly except the contents of the cell is highlighted from the double click event

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class Untitled1 {
  public static void main(String[] args)
  {
    JFrame f = new JFrame("Big Table Model");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BigTableModel tm = new BigTableModel();
    JTable t = new JTable(tm);
    JScrollPane sp = new JScrollPane(t);
    f.getContentPane().add(sp);
    f.pack();
    f.setVisible(true);
  }

  public static class BigTableModel extends AbstractTableModel {
    public boolean isCellEditable(int row,int column)
    {
      return(true);
    }

    public Object getValueAt(int row,int column)
    {
      return("" + row + "," + column);
    }

    public int getColumnCount()
    {
      return(10);
    }

    public int getRowCount()
    {
      return(1000);
    }
  }
}

---------- END SOURCE ----------

Release Regression From : 1.4.2_07
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.
###@###.### 2005-04-29 10:32:10 GMT

Comments
EVALUATION A fix has been determined. When JTree or JTable start dispatching events to a JTextComponent cell editor, they will set a client property on that editor with the number of clicks that it took to start editing. Accordingly, DefaultCaret will subtract the appropriate number of clicks in its processing.
13-04-2006

WORK AROUND The following small class can be used to work-around this problem: To make a JTable NOT select the text when double-clicking to edit a cell, simply add the call: new SelectionFixer(table); Here's the class: public class SelectionFixer extends MouseAdapter implements PropertyChangeListener { private JTextField comp; private JTable table; public SelectionFixer(JTable table) { this.table = table; table.addPropertyChangeListener("tableCellEditor", this); } public void mousePressed(MouseEvent me) { if (comp != null) { int pos = comp.viewToModel(me.getPoint()); comp.select(pos, pos); comp.removeMouseListener(this); comp = null; } } public void propertyChange(PropertyChangeEvent pce) { Object newVal = pce.getNewValue(); if (comp != null) { comp.removeMouseListener(this); comp = null; } if (newVal instanceof DefaultCellEditor) { DefaultCellEditor editor = (DefaultCellEditor)newVal; if (editor.getComponent() instanceof JTextField) { comp = (JTextField)editor.getComponent(); comp.addMouseListener(this); } } } }
12-08-2005

EVALUATION Work-around has now been added to the work-around section.
12-08-2005

EVALUATION The submitter is correct in that this is a regression with respect to 1.4.2. The behavior that they've described is also what I've been able to reproduce. That is, double clicking a cell in 1.4.2 simply positions the cursor whereas in 1.5 it selects the word underneath the mouse. The submitter's characterization of the cause of this bug isn't entirely accurate. In both 1.4.2 and 1.5.0 the mouse PRESS event is sent to the editor with a click count of 2. In neither release is a mouse CLICK event sent. There has been no change here. What has changed is that DefaultCaret, responsible for selecting text in JTextComponents was changed by the fix for 4912842 to initiate selection on the PRESS event where it used to do so on the CLICK event. This change was desirable for text components. It may also be considered desirable for JTable editors. I don't beleive it should be changed back. However, with more demand (votes) we'll consider adding a property to control this. For now, I have a work-around for users that encounter this. I am having it reviewed and will post it very shortly.
11-08-2005