8182577: Exception when Tab key moves focus to a JCheckbox with a custom ButtonModel
authorpsadhukhan
Sat, 01 Jul 2017 10:00:15 +0530
changeset 47145 392def3f8452
parent 47144 20f16e21003f
child 47146 9910fab09c00
8182577: Exception when Tab key moves focus to a JCheckbox with a custom ButtonModel Reviewed-by: ssadetsky, serb, kcr
jdk/src/java.desktop/share/classes/javax/swing/ButtonModel.java
jdk/src/java.desktop/share/classes/javax/swing/JToggleButton.java
jdk/src/java.desktop/share/classes/javax/swing/LayoutFocusTraversalPolicy.java
jdk/test/javax/swing/DefaultButtonModel/DefaultButtonModelCrashTest.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/ButtonModel.java	Sat Jul 01 09:56:02 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/ButtonModel.java	Sat Jul 01 10:00:15 2017 +0530
@@ -199,6 +199,21 @@
     public void setGroup(ButtonGroup group);
 
     /**
+     * Returns the group that the button belongs to.
+     * Normally used with radio buttons, which are mutually
+     * exclusive within their group.
+     *
+     * @implSpec The default implementation of this method returns {@code null}.
+     * Subclasses should return the group set by setGroup().
+     *
+     * @return the <code>ButtonGroup</code> that the button belongs to
+     * @since 10
+     */
+    default ButtonGroup getGroup() {
+        return null;
+    }
+
+    /**
      * Adds an <code>ActionListener</code> to the model.
      *
      * @param l the listener to add
--- a/jdk/src/java.desktop/share/classes/javax/swing/JToggleButton.java	Sat Jul 01 09:56:02 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/JToggleButton.java	Sat Jul 01 10:00:15 2017 +0530
@@ -219,8 +219,8 @@
           case TRAVERSAL_BACKWARD:
             ButtonModel model = getModel();
             JToggleButton selection = this;
-            if (model instanceof DefaultButtonModel) {
-                ButtonGroup group = ((DefaultButtonModel) model).getGroup();
+            if (model != null) {
+                ButtonGroup group = model.getGroup();
                 if (group != null && group.getSelection() != null
                                                   && !group.isSelected(model)) {
                     Iterator<AbstractButton> iterator =
--- a/jdk/src/java.desktop/share/classes/javax/swing/LayoutFocusTraversalPolicy.java	Sat Jul 01 09:56:02 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/LayoutFocusTraversalPolicy.java	Sat Jul 01 10:00:15 2017 +0530
@@ -238,19 +238,18 @@
         } else if (aComponent instanceof JComponent) {
             if (SunToolkit.isInstanceOf(aComponent,
                                                  "javax.swing.JToggleButton")) {
-                JToggleButton.ToggleButtonModel model =
-                        (JToggleButton.ToggleButtonModel) ((JToggleButton)
-                                aComponent).getModel();
+                ButtonModel model = ((JToggleButton)aComponent).getModel();
                 if (model != null) {
                     ButtonGroup group = model.getGroup();
                     if (group != null) {
                         Enumeration<AbstractButton> elements =
-                                                            group.getElements();
+                                                        group.getElements();
                         int idx = 0;
                         while (elements.hasMoreElements()) {
                             AbstractButton member = elements.nextElement();
-                            if (member.isVisible() && member.isDisplayable() &&
-                                   member.isEnabled() && member.isFocusable()) {
+                            if (member instanceof JToggleButton &&
+                                 member.isVisible() && member.isDisplayable() &&
+                                 member.isEnabled() && member.isFocusable()) {
                                 if (member == aComponent) {
                                     return idx == 0;
                                 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/DefaultButtonModel/DefaultButtonModelCrashTest.java	Sat Jul 01 10:00:15 2017 +0530
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/*
+ * @test
+ * @bug 8182577
+ * @summary  Verifies if moving focus via custom ButtonModel causes crash
+ * @run main DefaultButtonModelCrashTest
+ */
+import java.awt.BorderLayout;
+import java.awt.Container;
+import java.awt.Point;
+import java.awt.Robot;
+import java.awt.event.KeyEvent;
+import javax.swing.ButtonModel;
+import javax.swing.DefaultButtonModel;
+import javax.swing.JCheckBox;
+import javax.swing.JComponent;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+import javax.swing.SwingUtilities;
+
+public class DefaultButtonModelCrashTest {
+    private JFrame frame = null;
+    private JPanel panel;
+    private volatile Point p = null;
+
+    public static void main(String[] args) throws Exception {
+        new DefaultButtonModelCrashTest();
+    }
+
+    public DefaultButtonModelCrashTest() throws Exception {
+        try {
+            Robot robot = new Robot();
+            robot.setAutoDelay(200);
+            SwingUtilities.invokeAndWait(() -> go());
+            robot.waitForIdle();
+            robot.keyPress(KeyEvent.VK_TAB);
+            robot.keyRelease(KeyEvent.VK_TAB);
+            robot.delay(100);
+            robot.keyPress(KeyEvent.VK_TAB);
+            robot.keyRelease(KeyEvent.VK_TAB);
+        } finally {
+            SwingUtilities.invokeAndWait(()->frame  .dispose());
+        }
+    }
+
+    private void go() {
+
+        frame = new JFrame();
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        Container contentPane = frame.getContentPane();
+        ButtonModel model = new DefaultButtonModel();
+
+        JCheckBox check = new JCheckBox("a bit broken");
+        check.setModel(model);
+        panel = new JPanel(new BorderLayout());
+        panel.add(new JTextField("Press Tab (twice?)"), BorderLayout.NORTH);
+        panel.add(check);
+        contentPane.add(panel);
+        frame.setLocationRelativeTo(null);
+        frame.pack();
+        frame.setVisible(true);
+    }
+}