Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

8259237: Demo selection changes with left/right arrow key. No need to press space for selection. #99

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -762,8 +762,11 @@ else if (!srcFound) {
}

/**
* Find the new toggle button that focus needs to be
* Find the new toggle/radio button that focus needs to be
* moved to in the group, select the button
* In case of radio button, setPressed and setArmed is called
* on the button model, so that Action set on button is performed
* on selecting the button
*
* @param next, indicate if it's arrow up/left or down/right
*/
Expand All @@ -785,12 +788,16 @@ void selectNewButton(boolean next) {
if (newSelectedBtn != null &&
(newSelectedBtn != activeBtn)) {
ButtonModel btnModel = newSelectedBtn.getModel();
btnModel.setPressed(true);
btnModel.setArmed(true);
if (newSelectedBtn instanceof JRadioButton) {
btnModel.setPressed(true);
btnModel.setArmed(true);
}
newSelectedBtn.requestFocusInWindow();
newSelectedBtn.setSelected(true);
btnModel.setPressed(false);
btnModel.setArmed(false);
if (newSelectedBtn instanceof JRadioButton) {
btnModel.setPressed(false);
btnModel.setArmed(false);
}
}
}
}
Expand Down
Expand Up @@ -24,13 +24,15 @@
/*
* @test
* @key headful
* @bug 8249548
* @bug 8249548 8259237
* @summary Test focus traversal in button group containing JToggleButton
* and JRadioButton
* @run main TestButtonGroupFocusTraversal
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to append this bugid as product code is changed for which this testcase is modified

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please have a look.

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
Expand All @@ -43,12 +45,16 @@
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

public class TestButtonGroupFocusTraversal {
private static JFrame frame;
private static JTextField textFieldFirst, textFieldLast;
private static JToggleButton toggleButton1, toggleButton2;
private static JCheckBox checkBox1, checkBox2;
private static boolean toggleButtonActionPerformed;
private static boolean checkboxActionPerformed;
private static JRadioButton radioButton1, radioButton2;
private static Robot robot;

Expand All @@ -75,6 +81,36 @@ public void run() {
toggleButton2 = new JToggleButton("2");
radioButton1 = new JRadioButton("1");
radioButton2 = new JRadioButton("2");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be safe please check the JCheckBox, it also extends the JToggleButton and we should not break it.

checkBox1 = new JCheckBox("1");
checkBox2 = new JCheckBox("2");

toggleButton1.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
toggleButtonActionPerformed = true;
}
});

toggleButton2.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
toggleButtonActionPerformed = true;
}
});

checkBox1.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
checkboxActionPerformed = true;
}
});

checkBox2.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
checkboxActionPerformed = true;
}
});

ButtonGroup toggleGroup = new ButtonGroup();
toggleGroup.add(toggleButton1);
Expand All @@ -84,8 +120,13 @@ public void run() {
radioGroup.add(radioButton1);
radioGroup.add(radioButton2);

ButtonGroup checkboxButtonGroup = new ButtonGroup();
checkboxButtonGroup.add(checkBox1);
checkboxButtonGroup.add(checkBox2);

toggleButton2.setSelected(true);
radioButton2.setSelected(true);
checkBox2.setSelected(true);

frame = new JFrame("Test");
frame.setLayout(new FlowLayout());
Expand All @@ -96,6 +137,8 @@ public void run() {
pane.add(toggleButton2);
pane.add(radioButton1);
pane.add(radioButton2);
pane.add(checkBox1);
pane.add(checkBox2);
pane.add(textFieldLast);

frame.pack();
Expand Down Expand Up @@ -127,6 +170,20 @@ private static void checkFocusedComponent (Component component) {
}
}

private static void checkToggleButtonActionPerformed() {
if (toggleButtonActionPerformed) {
throw new RuntimeException("Toggle Button Action should not be" +
"performed");
}
}

private static void checkCheckboxActionPerformed() {
if (toggleButtonActionPerformed) {
throw new RuntimeException("Checkbox Action should not be" +
"performed");
}
}

public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoDelay(100);
Expand Down Expand Up @@ -164,9 +221,15 @@ public static void main(String[] args) throws Exception {
pressKey(KeyEvent.VK_TAB);
checkFocusedComponent(radioButton2);

pressKey(KeyEvent.VK_TAB);
checkFocusedComponent(checkBox2);

pressKey(KeyEvent.VK_TAB);
checkFocusedComponent(textFieldLast);

pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
checkFocusedComponent(checkBox2);

pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
checkFocusedComponent(radioButton2);

Expand All @@ -181,15 +244,19 @@ public static void main(String[] args) throws Exception {

pressKey(KeyEvent.VK_LEFT);
checkFocusedComponent(toggleButton1);
checkToggleButtonActionPerformed();

pressKey(KeyEvent.VK_RIGHT);
checkFocusedComponent(toggleButton2);
checkToggleButtonActionPerformed();

pressKey(KeyEvent.VK_UP);
checkFocusedComponent(toggleButton1);
checkToggleButtonActionPerformed();

pressKey(KeyEvent.VK_DOWN);
checkFocusedComponent(toggleButton2);
checkToggleButtonActionPerformed();

pressKey(KeyEvent.VK_TAB);
checkFocusedComponent(radioButton2);
Expand All @@ -206,6 +273,24 @@ public static void main(String[] args) throws Exception {
pressKey(KeyEvent.VK_DOWN);
checkFocusedComponent(radioButton2);

pressKey(KeyEvent.VK_TAB);
checkFocusedComponent(checkBox2);

pressKey(KeyEvent.VK_LEFT);
checkCheckboxActionPerformed();
checkFocusedComponent(checkBox1);

pressKey(KeyEvent.VK_RIGHT);
checkCheckboxActionPerformed();
checkFocusedComponent(checkBox2);

pressKey(KeyEvent.VK_UP);
checkCheckboxActionPerformed();
checkFocusedComponent(checkBox1);

pressKey(KeyEvent.VK_DOWN);
checkCheckboxActionPerformed();
checkFocusedComponent(checkBox2);
} finally {
if (frame != null) {
SwingUtilities.invokeAndWait(frame::dispose);
Expand All @@ -214,4 +299,3 @@ public static void main(String[] args) throws Exception {
}
}
}