Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6349059

JTable does not stop cell editing when it loses focus

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • client-libs

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



      ADDITIONAL OS VERSION INFORMATION :
      Windows 2000 Build 2195 Service Pack 4 (I've seen this bug in every version of Windows I've used. Haven't thought to check it in other OS's.)

      A DESCRIPTION OF THE PROBLEM :
      JTable should stop editing the current cell when it loses focus.

      Current behavior is that if the user selects a cell and begins editing, then clicks on something outside the table, the cell shows the value they entered, but this value is never posted back to the table model. This gives very non-intuitive behavior to the user: It looks like they've made an update, but the update is ignored.

      When focus leaves a JTable, it should stopCellEditing on the current cell.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See the test case below. This displays a JTable with 3 cells, a button labeled "Add", and a JLabel.

      Type a number into the first cell. Down arrow to the second cell. Type a number in the second cell. Down arrow to the third cell. Click the "Add" button. The program displays the sum of the two numbers you have entered. Cool.

      Now select either of the two cells with numbers in them. Change this number to a different value, but do NOT use arrow keys or tab to leave the cell and do not press enter -- leave the cursor on that cell. Now click the "Add" button. The total displayed is NOT CHANGED. Even though the user has entered a different value in the cell, because he did not move to a different cell within the JTable before exiting the JTable completely, the update was never posted back to the table model. This is very non-intuitive because the unposted value is nevertheless visible on the screen: it looks like the program calculated the total incorrectly.

      This happens

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When the user clicks on something outside the JTable, I expect any udates to the current cell to be posted to the table model.
      ACTUAL -
      The user's last change is never posted to the table model.

      This happens when the JTable is created using either the JTable(Object[][] data,Object[] heads) and JTable(TableModel tm).

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      /* This is, as I presume you realize, not a production-quality program.
      There's no error-checking, etc.
      */

      public class Dork extends JFrame implements ActionListener
      {
      String[][] data;
      JLabel total;

      static public void main(String[] args)
      {
      new Dork();
      }

      Dork()
      {
      super("Dork");
      Container content=getContentPane();

      String[] heads=new String[] {"Values"};
      data=new String[3][1];
      JTable table=new JTable(data,heads);
      content.add(table,BorderLayout.CENTER);

      JButton add=new JButton("Add");
      add.addActionListener(this);
      content.add(add,BorderLayout.EAST);

      total=new JLabel("(total)");
      content.add(total,BorderLayout.SOUTH);

      setDefaultCloseOperation(EXIT_ON_CLOSE);

      pack();
      show();
      }

      public void actionPerformed(ActionEvent e)
      {
      int calc=0;
      for (int x=0;x<3;++x)
      {
      if (data[x][0]!=null)
      calc+=Integer.parseInt(data[x][0]);
      }
      total.setText(Integer.toString(calc));
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      In Java 1.2 I subclassed JTable and in my version added a focus listener that, when the table lost focus, checked if a cell was being edited and if so stopped the edit. This code was broken by 1.3 because of changes in the way focus was handled. I made it work again but now I've discovered it broke again in 1.4.

            shickeysunw Shannon Hickey (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: