Skip to content

8074883: Tab key should move to focused button in a button group #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions jdk/src/share/classes/java/awt/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -7759,6 +7759,15 @@ private boolean isRequestFocusAccepted(boolean temporary,
}
}

boolean ret = Component.requestFocusController.acceptRequestFocus(focusOwner,
this,
temporary,
focusedWindowChangeAllowed,
cause);
if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
focusLog.finest("RequestFocusController returns {0}", ret);
}

if (focusOwner == this || focusOwner == null) {
// Controller is supposed to verify focus transfers and for this it
// should know both from and to components. And it shouldn't verify
Expand All @@ -7782,15 +7791,6 @@ private boolean isRequestFocusAccepted(boolean temporary,
return true;
}

boolean ret = Component.requestFocusController.acceptRequestFocus(focusOwner,
this,
temporary,
focusedWindowChangeAllowed,
cause);
if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
focusLog.finest("RequestFocusController returns {0}", ret);
}

return ret;
}

Expand Down
16 changes: 16 additions & 0 deletions jdk/src/share/classes/javax/swing/JComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,22 @@ public boolean acceptRequestFocus(Component from, Component to,
return true;
}

if (to instanceof JToggleButton) {
JToggleButton tb = ((JToggleButton)to).getGroupSelection(cause);
if (tb != to) {
if (focusedWindowChangeAllowed) {
SwingUtilities.invokeLater(() -> {
tb.requestFocus();
});
} else {
SwingUtilities.invokeLater(() -> {
tb.requestFocusInWindow();
});
}
return false;
}
}

if ((from == null) || !(from instanceof JComponent)) {
return true;
}
Expand Down
37 changes: 37 additions & 0 deletions jdk/src/share/classes/javax/swing/JToggleButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.io.ObjectInputStream;
import java.io.IOException;

import java.util.Enumeration;
import sun.awt.CausedFocusEvent;

/**
* An implementation of a two-state button.
Expand Down Expand Up @@ -208,6 +210,41 @@ boolean shouldUpdateSelectedStateFromAction() {
return true;
}

JToggleButton getGroupSelection(CausedFocusEvent.Cause cause) {
switch (cause) {
case ACTIVATION:
case TRAVERSAL:
case TRAVERSAL_UP:
case TRAVERSAL_DOWN:
case TRAVERSAL_FORWARD:
case TRAVERSAL_BACKWARD:
ButtonModel model = getModel();
JToggleButton selection = this;
if (model instanceof DefaultButtonModel) {
ButtonGroup group = ((DefaultButtonModel) model).getGroup();
if (group != null && group.getSelection() != null
&& !group.isSelected(model)) {
Enumeration<AbstractButton> iterator =
group.getElements();
while (iterator.hasMoreElements()) {
AbstractButton member = iterator.nextElement();
if (group.isSelected(member.getModel())) {
if (member instanceof JToggleButton &&
member.isVisible() && member.isDisplayable() &&
member.isEnabled() && member.isFocusable()) {
selection = (JToggleButton) member;
}
break;
}
}
}
}
return selection;
default:
return this;
}
}

// *********************************************************************

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2016, 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 8074883
* @summary Tab key should move to focused button in a button group
* @run main ButtonGroupFocusTest
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class ButtonGroupFocusTest {

private static JRadioButton button1;
private static JRadioButton button2;
private static JRadioButton button3;
private static JRadioButton button4;
private static JRadioButton button5;
private static Robot robot;
private static JFrame frame;

public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoDelay(100);

SwingUtilities.invokeAndWait(() -> {
frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
button1 = new JRadioButton("Button 1");
contentPane.add(button1);
button2 = new JRadioButton("Button 2");
contentPane.add(button2);
button3 = new JRadioButton("Button 3");
contentPane.add(button3);
button4 = new JRadioButton("Button 4");
contentPane.add(button4);
button5 = new JRadioButton("Button 5");
contentPane.add(button5);
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);

group = new ButtonGroup();
group.add(button4);
group.add(button5);

button2.setSelected(true);

frame.pack();
frame.setVisible(true);
});

robot.waitForIdle();
robot.delay(200);

SwingUtilities.invokeAndWait(() -> {
if( !button2.hasFocus() ) {
frame.dispose();
throw new RuntimeException(
"Button 2 should get focus after activation");
}
});

robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);

robot.waitForIdle();
robot.delay(200);

SwingUtilities.invokeAndWait(() -> {
if( !button4.hasFocus() ) {
frame.dispose();
throw new RuntimeException(
"Button 4 should get focus");
}
button3.setSelected(true);
});

robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);

robot.waitForIdle();
robot.delay(200);

SwingUtilities.invokeAndWait(() -> {
if( !button3.hasFocus() ) {
frame.dispose();
throw new RuntimeException(
"selected Button 3 should get focus");
}
});

SwingUtilities.invokeLater(frame::dispose);
}
}