在JRadioButtons的ButtonGroup中获得焦点,以转到当前选定的项目,而不是第一个

Pixelstix

我有一些JRadioButtonsButtonGroup这些都在容器内,并且组中还有其他项目。每当我切换到按钮组时,焦点始终移至该组中的第一项。但是,我宁愿去选择的项目。

要重现该问题,请使用下面的代码(添加导入,现在暂时忽略我懒得将其全部放在SwingUtilities.invokeLater电话中这一事实)。向下移至单选按钮,然后向下移至较晚的一项,例如“蓝色”。再按4次返回单选按钮,您将发现焦点位于顶部的“红色”单选按钮上。我希望重点放在“蓝色”单选按钮上。

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);
}
rai

您可能可以使用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);
    });
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Orbeon Forms中,为什么将焦点设置为第一个输入字段而不是第一个字段?

来自分类Dev

根据当前数组中每个项目的第一个元素创建新数组

来自分类Dev

如何在JRadioButtons的ButtonGroup中随机选择一个按钮?

来自分类Dev

SelectList上的选定项目返回到第一个项目

来自分类Dev

SelectList上的选定项目返回到第一个项目

来自分类Dev

在div中获得第一个div

来自分类Dev

在mongoengine中获得第一个对象

来自分类Dev

如何在路由历史记录中获得与当前URL不同的第一个URL?

来自分类Dev

获得第一个价值

来自分类Dev

在第一个列表分页中显示项目

来自分类Dev

Listbox.items [i] .Selected仅捕获第一个选定的项目

来自分类Dev

Listview替换第一个项目,而不是创建新项目

来自分类Dev

检索表时,无法在表中的第一个位置获得第一个元素?

来自分类Dev

从 angularjs 表单中的第一个无效输入中移除焦点

来自分类Dev

跳转到emacs中的第一个非空白字符

来自分类Dev

如何转到文件中的第一个搜索结果?

来自分类Dev

使按钮转到第二个选项卡式场景,而不是第一个?

来自分类Dev

尝试遍历第一个对象并将当前项目添加到另一个对象中

来自分类Dev

在Laravel中读取数组的第一个记录而不是第一个字符

来自分类Dev

我如何获得按MySQL查询分组的最后结果(不是第一个)

来自分类Dev

JavaScript无法将焦点设置为ul中的第一个li元素

来自分类Dev

将焦点设置为AngularJS中的第一个无效表单元素

来自分类Dev

无法将CurrentCell焦点设置为DataGridView中的第一个可编辑列

来自分类Dev

如何在angular js指令中启用第一个输入焦点

来自分类Dev

从 http GET 获得第一个结果后立即显示项目

来自分类Dev

显示第一个选定的行Mysql

来自分类Dev

如何使 apply() 在函数的一个参数(不是第一个)中传递对象?

来自分类Dev

将第一个提交存储在一个空的项目存储库中

来自分类Dev

最佳实践:处理foreach中的第一个/最后一个项目

Related 相关文章

  1. 1

    在Orbeon Forms中,为什么将焦点设置为第一个输入字段而不是第一个字段?

  2. 2

    根据当前数组中每个项目的第一个元素创建新数组

  3. 3

    如何在JRadioButtons的ButtonGroup中随机选择一个按钮?

  4. 4

    SelectList上的选定项目返回到第一个项目

  5. 5

    SelectList上的选定项目返回到第一个项目

  6. 6

    在div中获得第一个div

  7. 7

    在mongoengine中获得第一个对象

  8. 8

    如何在路由历史记录中获得与当前URL不同的第一个URL?

  9. 9

    获得第一个价值

  10. 10

    在第一个列表分页中显示项目

  11. 11

    Listbox.items [i] .Selected仅捕获第一个选定的项目

  12. 12

    Listview替换第一个项目,而不是创建新项目

  13. 13

    检索表时,无法在表中的第一个位置获得第一个元素?

  14. 14

    从 angularjs 表单中的第一个无效输入中移除焦点

  15. 15

    跳转到emacs中的第一个非空白字符

  16. 16

    如何转到文件中的第一个搜索结果?

  17. 17

    使按钮转到第二个选项卡式场景,而不是第一个?

  18. 18

    尝试遍历第一个对象并将当前项目添加到另一个对象中

  19. 19

    在Laravel中读取数组的第一个记录而不是第一个字符

  20. 20

    我如何获得按MySQL查询分组的最后结果(不是第一个)

  21. 21

    JavaScript无法将焦点设置为ul中的第一个li元素

  22. 22

    将焦点设置为AngularJS中的第一个无效表单元素

  23. 23

    无法将CurrentCell焦点设置为DataGridView中的第一个可编辑列

  24. 24

    如何在angular js指令中启用第一个输入焦点

  25. 25

    从 http GET 获得第一个结果后立即显示项目

  26. 26

    显示第一个选定的行Mysql

  27. 27

    如何使 apply() 在函数的一个参数(不是第一个)中传递对象?

  28. 28

    将第一个提交存储在一个空的项目存储库中

  29. 29

    最佳实践:处理foreach中的第一个/最后一个项目

热门标签

归档