changeset 17773:e1e784e7fe35

8182577: Exception when Tab key moves focus to a JCheckbox with a custom ButtonModel Reviewed-by: ssadetsky, serb, kcr
author psadhukhan
date Sat, 01 Jul 2017 10:00:15 +0530
parents 19098b855e42
children e11953665ba7
files src/java.desktop/share/classes/javax/swing/ButtonModel.java src/java.desktop/share/classes/javax/swing/JToggleButton.java src/java.desktop/share/classes/javax/swing/LayoutFocusTraversalPolicy.java test/javax/swing/DefaultButtonModel/DefaultButtonModelCrashTest.java
diffstat 4 files changed, 107 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/share/classes/javax/swing/ButtonModel.java	Sat Jul 01 09:56:02 2017 +0530
+++ b/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/src/java.desktop/share/classes/javax/swing/JToggleButton.java	Sat Jul 01 09:56:02 2017 +0530
+++ b/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/src/java.desktop/share/classes/javax/swing/LayoutFocusTraversalPolicy.java	Sat Jul 01 09:56:02 2017 +0530
+++ b/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/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);
+    }
+}