Skip to content

Commit 4e8e853

Browse files
committed
6788481: CellEditorListener.editingCanceled is never called
Reviewed-by: prr, azvegint, serb
1 parent 692bedb commit 4e8e853

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,9 @@ public void run() {
617617
}
618618
*/
619619
} else if (key == CANCEL_EDITING) {
620+
if (table.isEditing()) {
621+
table.getCellEditor().cancelCellEditing();
622+
}
620623
table.removeEditor();
621624
} else if (key == SELECT_ALL) {
622625
table.selectAll();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 6788481
26+
* @key headful
27+
* @summary Verifies if CellEditorListener.editingCanceled is called
28+
* @run main BugCellEditorListener
29+
*/
30+
31+
import java.awt.Robot;
32+
import java.awt.Point;
33+
import java.awt.Rectangle;
34+
import java.awt.event.InputEvent;
35+
import java.awt.event.KeyEvent;
36+
import javax.swing.DefaultCellEditor;
37+
import javax.swing.JFrame;
38+
import javax.swing.JScrollPane;
39+
import javax.swing.JTable;
40+
import javax.swing.SwingUtilities;
41+
import javax.swing.UIManager;
42+
import javax.swing.UnsupportedLookAndFeelException;
43+
import javax.swing.event.CellEditorListener;
44+
import javax.swing.event.ChangeEvent;
45+
46+
public class BugCellEditorListener {
47+
static Robot robot;
48+
static JFrame frame;
49+
static JTable table;
50+
static volatile Point pt;
51+
static volatile boolean cancelled;
52+
53+
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
54+
try {
55+
UIManager.setLookAndFeel(laf.getClassName());
56+
} catch (UnsupportedLookAndFeelException ignored) {
57+
System.out.println("Unsupported L&F: " + laf.getClassName());
58+
} catch (ClassNotFoundException | InstantiationException
59+
| IllegalAccessException e) {
60+
throw new RuntimeException(e);
61+
}
62+
}
63+
64+
public static void main(String [] args) throws Exception {
65+
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
66+
System.out.println("Testing l&f : " + laf.getClassName());
67+
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
68+
test();
69+
}
70+
}
71+
72+
private static void test() throws Exception {
73+
try {
74+
robot = new Robot();
75+
robot.setAutoDelay(100);
76+
SwingUtilities.invokeAndWait(() -> {
77+
frame = new JFrame();
78+
table = new JTable(5, 5);
79+
table.setPreferredScrollableViewportSize(table.getPreferredSize());
80+
JScrollPane scrollpane = new JScrollPane(table);
81+
frame.getContentPane().add(scrollpane);
82+
83+
DefaultCellEditor dce = (DefaultCellEditor) table.getDefaultEditor(Object.class);
84+
dce.addCellEditorListener(new CellEditorListener() {
85+
public void editingStopped(ChangeEvent e) {
86+
System.out.println("stopped");
87+
}
88+
89+
public void editingCanceled(ChangeEvent e) {
90+
System.out.println("canceled");
91+
cancelled = true;
92+
}
93+
});
94+
95+
96+
frame.pack();
97+
frame.setLocationRelativeTo(null);
98+
frame.setVisible(true);
99+
});
100+
robot.waitForIdle();
101+
robot.delay(1000);
102+
SwingUtilities.invokeAndWait(() -> {
103+
Rectangle rect = table.getCellRect(2, 0, false);
104+
pt = new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
105+
SwingUtilities.convertPointToScreen(pt, table);
106+
});
107+
robot.mouseMove(pt.x, pt.y);
108+
robot.waitForIdle();
109+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
110+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
111+
robot.waitForIdle();
112+
robot.keyPress(KeyEvent.VK_A);
113+
robot.keyRelease(KeyEvent.VK_A);
114+
robot.keyPress(KeyEvent.VK_A);
115+
robot.keyRelease(KeyEvent.VK_A);
116+
robot.waitForIdle();
117+
robot.keyPress(KeyEvent.VK_ESCAPE);
118+
robot.keyRelease(KeyEvent.VK_ESCAPE);
119+
robot.waitForIdle();
120+
robot.delay(1000);
121+
if (!cancelled) {
122+
throw new RuntimeException("Cell editing cancel listener not called");
123+
}
124+
} finally {
125+
SwingUtilities.invokeAndWait(() -> {
126+
if (frame != null) {
127+
frame.dispose();
128+
}
129+
});
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)