如何将 JRadioButton 组与模型一起使用

发布于 2024-08-17 13:51:10 字数 165 浏览 5 评论 0原文

有没有什么方法可以将一组 JRadioButtons 与数据模型关联起来,以便更容易判断选择了哪个按钮(如果有)?

在理想的情况下,我希望将一组 N 个单选按钮与一个 enum 类关联起来,该类具有一个 NONE 值以及与每个单选按钮关联的一个值。

Is there any way to associate a group of JRadioButtons with a data model so it is easier to tell which button (if any) is selected?

In an ideal world, I would like to associate a group of N radiobuttons with an enum class that has a NONE value and one value associated with each radiobutton.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-08-24 13:51:10

我解决了我自己的问题,这并不太难,所以分享并享受:

import java.util.EnumMap;
import java.util.Map;
import javax.swing.JRadioButton;

public class RadioButtonGroupEnumAdapter<E extends Enum<E>> {
    final private Map<E, JRadioButton> buttonMap;

    public RadioButtonGroupEnumAdapter(Class<E> enumClass)
    {
        this.buttonMap = new EnumMap<E, JRadioButton>(enumClass);
    }
    public void importMap(Map<E, JRadioButton> map)
    {
        for (E e : map.keySet())
        {
            this.buttonMap.put(e, map.get(e));
        }
    }
    public void associate(E e, JRadioButton btn)
    {
        this.buttonMap.put(e, btn);
    }
    public E getValue()
    {
        for (E e : this.buttonMap.keySet())
        {
            JRadioButton btn = this.buttonMap.get(e);
            if (btn.isSelected())
            {
                return e;
            }
        }
        return null;
    }
    public void setValue(E e)
    {
        JRadioButton btn = (e == null) ? null : this.buttonMap.get(e);
        if (btn == null)
        {
            // the following doesn't seem efficient...
                    // but since when do we have more than say 10 radiobuttons?
            for (JRadioButton b : this.buttonMap.values())
            {
                b.setSelected(false);
            }

        }
        else
        {
            btn.setSelected(true);
        }
    }
}

I solved my own problem, this wasn't too hard, so share and enjoy:

import java.util.EnumMap;
import java.util.Map;
import javax.swing.JRadioButton;

public class RadioButtonGroupEnumAdapter<E extends Enum<E>> {
    final private Map<E, JRadioButton> buttonMap;

    public RadioButtonGroupEnumAdapter(Class<E> enumClass)
    {
        this.buttonMap = new EnumMap<E, JRadioButton>(enumClass);
    }
    public void importMap(Map<E, JRadioButton> map)
    {
        for (E e : map.keySet())
        {
            this.buttonMap.put(e, map.get(e));
        }
    }
    public void associate(E e, JRadioButton btn)
    {
        this.buttonMap.put(e, btn);
    }
    public E getValue()
    {
        for (E e : this.buttonMap.keySet())
        {
            JRadioButton btn = this.buttonMap.get(e);
            if (btn.isSelected())
            {
                return e;
            }
        }
        return null;
    }
    public void setValue(E e)
    {
        JRadioButton btn = (e == null) ? null : this.buttonMap.get(e);
        if (btn == null)
        {
            // the following doesn't seem efficient...
                    // but since when do we have more than say 10 radiobuttons?
            for (JRadioButton b : this.buttonMap.values())
            {
                b.setSelected(false);
            }

        }
        else
        {
            btn.setSelected(true);
        }
    }
}
墨洒年华 2024-08-24 13:51:10

javax.swing.ButtonGroup 是否位于您要查找的内容

http://java.sun.com/javase/6/docs/api/javax/swing/ButtonGroup.html

Is the javax.swing.ButtonGroup on the lines of what you're looking for

http://java.sun.com/javase/6/docs/api/javax/swing/ButtonGroup.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文