在 JRadioButtons 的 ButtonGroup 中获取焦点以转到当前选定的项目而不是第一个
Get the focus in a ButtonGroup of JRadioButtons to go to the currently selected item instead of first
我在 ButtonGroup
中有一些 JRadioButtons
。这些都在一个容器里面,组里还有其他的项目。每当我切换到按钮组时,焦点总是转到组中的第一个项目。但是,我更希望它转到所选的项目。
要重现这个问题,请使用下面的代码(添加导入,现在忽略我懒得把它全部放在一个 SwingUtilities.invokeLater
调用中的事实)。向下选择单选按钮,然后向下箭头到后面的项目之一,例如蓝色。再次按 Tab 4 返回到单选按钮,您会发现焦点在顶部 "Red" 单选按钮上。我希望焦点位于 "Blue" 单选按钮上。
public static void main(String[] args)
{
JFrame f = new JFrame("JRadioButton Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container parent = f.getContentPane();
parent.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 1;
parent.add(new JLabel("Start"), gbc);
gbc.gridx++;
parent.add(new JTextField(10), gbc);
gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("End"), gbc);
gbc.gridx++;
parent.add(new JTextField(10), gbc);
gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("Colors"), gbc);
gbc.gridx++;
final Box buttons = Box.createVerticalBox();
parent.add(buttons, gbc);
final ButtonGroup bg = new ButtonGroup();
for (String s : "Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(","))
{
JRadioButton radioBtn = new JRadioButton(s);
buttons.add(radioBtn);
radioBtn.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e)
{
System.out.printf("Itemstate changed to %s\n",
e.getStateChange() == ItemEvent.SELECTED ? "SELECTED" : "DESELECTED");
}
});
radioBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("JRadioButton action listener");
}
});
bg.add(radioBtn);
}
gbc.gridx = 1;
gbc.gridy += 2;
gbc.gridwidth = 2;
final JLabel currentValue = new JLabel("none");
parent.add(currentValue, gbc);
gbc.gridy--;
JButton btn = new JButton("Show Button Group Value");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
ButtonModel selection = bg.getSelection();
for (int i = 0; i < buttons.getComponentCount(); i++)
{
JRadioButton radioBtn = (JRadioButton)buttons.getComponent(i);
ButtonModel loopModel = radioBtn.getModel();
if (loopModel == selection)
{
currentValue.setText(radioBtn.getText());
return;
}
currentValue.setText("none");
}
}
});
parent.add(btn, gbc);
f.pack();
f.setVisible(true);
}
您或许可以使用 FocusTraversalPolicy
:
buttons.setFocusTraversalPolicyProvider(true);
buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
@Override public Component getDefaultComponent(Container focusCycleRoot) {
ButtonModel selection = bg.getSelection();
for (Component c: focusCycleRoot.getComponents()) {
JRadioButton radioBtn = (JRadioButton) c;
ButtonModel loopModel = radioBtn.getModel();
if (loopModel == selection) {
return radioBtn;
}
}
return super.getDefaultComponent(focusCycleRoot);
}
});
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonGroupFocusTraversalTest {
public JComponent makeUI() {
JPanel parent = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 1;
parent.add(new JLabel("Start"), gbc);
gbc.gridx++;
parent.add(new JTextField(10), gbc);
gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("End"), gbc);
gbc.gridx++;
JTextField textField = new JTextField(10);
parent.add(textField, gbc);
gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("Colors"), gbc);
gbc.gridx++;
final Box buttons = Box.createVerticalBox();
parent.add(buttons, gbc);
final ButtonGroup bg = new ButtonGroup();
for (String s : "Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(",")) {
JRadioButton radioBtn = new JRadioButton(s);
buttons.add(radioBtn);
bg.add(radioBtn);
}
gbc.gridx = 1;
gbc.gridy += 2;
gbc.gridwidth = 2;
final JLabel currentValue = new JLabel("none");
parent.add(currentValue, gbc);
gbc.gridy--;
JButton btn = new JButton("Show Button Group Value");
parent.add(btn, gbc);
buttons.setFocusTraversalPolicyProvider(true);
buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
@Override public Component getDefaultComponent(Container focusCycleRoot) {
ButtonModel selection = bg.getSelection();
for (Component c: focusCycleRoot.getComponents()) {
JRadioButton radioBtn = (JRadioButton) c;
ButtonModel loopModel = radioBtn.getModel();
if (loopModel == selection) {
return radioBtn;
}
}
return super.getDefaultComponent(focusCycleRoot);
}
});
return parent;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ButtonGroupFocusTraversalTest().makeUI());
f.setSize(320, 320);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
buttonGroup.setFocus(selectedRadioButton);
试试这个。此 将焦点设置在 selectedRadioButton 而不是第一个
我在 ButtonGroup
中有一些 JRadioButtons
。这些都在一个容器里面,组里还有其他的项目。每当我切换到按钮组时,焦点总是转到组中的第一个项目。但是,我更希望它转到所选的项目。
要重现这个问题,请使用下面的代码(添加导入,现在忽略我懒得把它全部放在一个 SwingUtilities.invokeLater
调用中的事实)。向下选择单选按钮,然后向下箭头到后面的项目之一,例如蓝色。再次按 Tab 4 返回到单选按钮,您会发现焦点在顶部 "Red" 单选按钮上。我希望焦点位于 "Blue" 单选按钮上。
public static void main(String[] args)
{
JFrame f = new JFrame("JRadioButton Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container parent = f.getContentPane();
parent.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 1;
parent.add(new JLabel("Start"), gbc);
gbc.gridx++;
parent.add(new JTextField(10), gbc);
gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("End"), gbc);
gbc.gridx++;
parent.add(new JTextField(10), gbc);
gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("Colors"), gbc);
gbc.gridx++;
final Box buttons = Box.createVerticalBox();
parent.add(buttons, gbc);
final ButtonGroup bg = new ButtonGroup();
for (String s : "Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(","))
{
JRadioButton radioBtn = new JRadioButton(s);
buttons.add(radioBtn);
radioBtn.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e)
{
System.out.printf("Itemstate changed to %s\n",
e.getStateChange() == ItemEvent.SELECTED ? "SELECTED" : "DESELECTED");
}
});
radioBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("JRadioButton action listener");
}
});
bg.add(radioBtn);
}
gbc.gridx = 1;
gbc.gridy += 2;
gbc.gridwidth = 2;
final JLabel currentValue = new JLabel("none");
parent.add(currentValue, gbc);
gbc.gridy--;
JButton btn = new JButton("Show Button Group Value");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
ButtonModel selection = bg.getSelection();
for (int i = 0; i < buttons.getComponentCount(); i++)
{
JRadioButton radioBtn = (JRadioButton)buttons.getComponent(i);
ButtonModel loopModel = radioBtn.getModel();
if (loopModel == selection)
{
currentValue.setText(radioBtn.getText());
return;
}
currentValue.setText("none");
}
}
});
parent.add(btn, gbc);
f.pack();
f.setVisible(true);
}
您或许可以使用 FocusTraversalPolicy
:
buttons.setFocusTraversalPolicyProvider(true);
buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
@Override public Component getDefaultComponent(Container focusCycleRoot) {
ButtonModel selection = bg.getSelection();
for (Component c: focusCycleRoot.getComponents()) {
JRadioButton radioBtn = (JRadioButton) c;
ButtonModel loopModel = radioBtn.getModel();
if (loopModel == selection) {
return radioBtn;
}
}
return super.getDefaultComponent(focusCycleRoot);
}
});
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonGroupFocusTraversalTest {
public JComponent makeUI() {
JPanel parent = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 1;
parent.add(new JLabel("Start"), gbc);
gbc.gridx++;
parent.add(new JTextField(10), gbc);
gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("End"), gbc);
gbc.gridx++;
JTextField textField = new JTextField(10);
parent.add(textField, gbc);
gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("Colors"), gbc);
gbc.gridx++;
final Box buttons = Box.createVerticalBox();
parent.add(buttons, gbc);
final ButtonGroup bg = new ButtonGroup();
for (String s : "Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(",")) {
JRadioButton radioBtn = new JRadioButton(s);
buttons.add(radioBtn);
bg.add(radioBtn);
}
gbc.gridx = 1;
gbc.gridy += 2;
gbc.gridwidth = 2;
final JLabel currentValue = new JLabel("none");
parent.add(currentValue, gbc);
gbc.gridy--;
JButton btn = new JButton("Show Button Group Value");
parent.add(btn, gbc);
buttons.setFocusTraversalPolicyProvider(true);
buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
@Override public Component getDefaultComponent(Container focusCycleRoot) {
ButtonModel selection = bg.getSelection();
for (Component c: focusCycleRoot.getComponents()) {
JRadioButton radioBtn = (JRadioButton) c;
ButtonModel loopModel = radioBtn.getModel();
if (loopModel == selection) {
return radioBtn;
}
}
return super.getDefaultComponent(focusCycleRoot);
}
});
return parent;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ButtonGroupFocusTraversalTest().makeUI());
f.setSize(320, 320);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
buttonGroup.setFocus(selectedRadioButton);
试试这个。此 将焦点设置在 selectedRadioButton 而不是第一个