This patch introduces ESCAPE key functionality to JTable editing - if
you are editing a cell and press escape editing is canceled (your
changes are discarded and focus goes back to the JTable).

Patch is attached.

Note: the JTable in the Swing Demo can show the new editing behavior.

2005-08-09  Anthony Balkissoon  <[EMAIL PROTECTED]>
        * javax/swing/JTable.java:
        (editingCancelled): Implemented.
        (editCellAt): Added a KeyListener to our editor to listen for
        the ESCAPE key and cancel editing upon receiving it.

-Tony
Index: javax/swing/JTable.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JTable.java,v
retrieving revision 1.40
diff -u -r1.40 JTable.java
--- javax/swing/JTable.java	9 Aug 2005 19:07:50 -0000	1.40
+++ javax/swing/JTable.java	9 Aug 2005 19:45:53 -0000
@@ -45,6 +45,8 @@
 import java.awt.Rectangle;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
 import java.text.DateFormat;
 import java.text.NumberFormat;
 import java.util.Date;
@@ -576,6 +578,12 @@
   Timer editorTimer = new EditorUpdateTimer();
 
   /**
+   * Stores the old value of a cell before it was edited, in case
+   * editing is cancelled
+   */
+  Object oldCellValue;
+
+  /**
    * Creates a new <code>JTable</code> instance.
    */
   public JTable ()
@@ -838,6 +846,20 @@
 
   public void editingCanceled (ChangeEvent event)
   {
+    if (rowBeingEdited > -1 && columnBeingEdited > -1)
+      {
+        if (getValueAt(rowBeingEdited, columnBeingEdited) instanceof JTextField)
+          {
+            remove ((Component)getValueAt(rowBeingEdited, columnBeingEdited));
+            setValueAt(oldCellValue, rowBeingEdited, columnBeingEdited);
+          }
+        rowBeingEdited = -1;
+        columnBeingEdited = -1;
+      }
+    editorTimer.stop();
+    editorComp = null;
+    cellEditor = null;
+    requestFocusInWindow(false);
     repaint();
   }
 
@@ -2297,8 +2319,17 @@
    */
   public boolean editCellAt (int row, int column)
   {
+    oldCellValue = getValueAt(row, column);
     setCellEditor(getCellEditor(row, column));
     editorComp = prepareEditor(cellEditor, row, column);
+    editorComp.addKeyListener(new KeyAdapter() {
+        public void keyPressed(KeyEvent e) {
+          if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
+            if (JTable.this.cellEditor != null)
+              JTable.this.cellEditor.cancelCellEditing();
+          }
+        }
+      });
     cellEditor.addCellEditorListener(this);
     rowBeingEdited = row;
     columnBeingEdited = column;
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to