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

Reviewed-by: psadhukhan, kizune, serb
This commit is contained in:
Pankaj Bansal 2021-01-12 09:46:06 +00:00
parent a7e5da22c4
commit 28ff2de186
2 changed files with 98 additions and 7 deletions

View File

@ -762,8 +762,11 @@ public class BasicButtonUI extends ButtonUI{
}
/**
* 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
*/
@ -785,12 +788,16 @@ public class BasicButtonUI extends ButtonUI{
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);
}
}
}
}

View File

@ -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
*/
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
@ -43,12 +45,16 @@ import java.awt.FlowLayout;
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;
@ -75,6 +81,36 @@ public class TestButtonGroupFocusTraversal {
toggleButton2 = new JToggleButton("2");
radioButton1 = new JRadioButton("1");
radioButton2 = new JRadioButton("2");
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);
@ -84,8 +120,13 @@ public class TestButtonGroupFocusTraversal {
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());
@ -96,6 +137,8 @@ public class TestButtonGroupFocusTraversal {
pane.add(toggleButton2);
pane.add(radioButton1);
pane.add(radioButton2);
pane.add(checkBox1);
pane.add(checkBox2);
pane.add(textFieldLast);
frame.pack();
@ -127,6 +170,20 @@ public class TestButtonGroupFocusTraversal {
}
}
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);
@ -164,9 +221,15 @@ public class TestButtonGroupFocusTraversal {
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);
@ -181,15 +244,19 @@ public class TestButtonGroupFocusTraversal {
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);
@ -206,6 +273,24 @@ public class TestButtonGroupFocusTraversal {
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);
@ -214,4 +299,3 @@ public class TestButtonGroupFocusTraversal {
}
}
}