【问题标题】:Deselecting a radio button belonging to a buttonGroup in NetBeans在 NetBeans 中取消选择属于 buttonGroup 的单选按钮
【发布时间】:2018-08-15 23:01:34
【问题描述】:

我在 NetBeans 中制作了一个包含大量单选按钮和按钮组的 UI。假设 2 个单选按钮分配给同一个按钮组。一旦您单击其中一个单选按钮,就无法取消选择它。如果用户再次单击它,我该怎么做才能取消选择 readiobuttons?这是我的尝试:

if (b1.isSelected()==true) {
   b1.setEnabled(false); 
}

但我不想最终禁用它。帮帮我。

编辑:这是一种便宜的方法:

int k=0;
private void mb1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
if (mb1.isSelected()==true) { //mb1 is the radiobutton
    k++;
}
if (k%2==0) {
    buttonGroup1.clearSelection();
}

是的,但这会使 UI 混乱,有时必须单击按钮组中的单选按钮两次才能再次选择它们。

【问题讨论】:

  • 我想知道你是否可以使用ButtonGroup#setSelected(ButtonModel, boolean)
  • 旁注:if(b1.isSelected()==true)if(b1.isSelected()) 是等效的,而后者在您这边引入错误的风险较小。
  • @MadProgrammer 我试过了。如果我这样做,它会被取消选择,但随后无法重新选择
  • stackoverflow.com/questions/43678934/… 我的问题与此类似,但它是用 C 编码的,最重要的是使用了“.tag”,它至少在 java 或 Netbeans 中不起作用

标签: java user-interface netbeans netbeans-8


【解决方案1】:

好的,这是一个小技巧。有很多问题很难解决。

ButtonGroup 是一个具体的类,因此制作自定义版本很困难(这就是我喜欢interfaces 的原因),它更加复杂,因为它非常依赖private(或包私有)属性,没有提供任何合理的扩展点

因此,这留下了两种行动方案,要么使用反射,要么复制基类并进行我们自己的修改——这两种情况都不愉快。

所以,在我复制的所有代码中,我只修改了一个方法...

public void setSelected(ButtonModel m, boolean b) {
    if (b) {
        if (m != selection) {
            ButtonModel oldSelection = selection;
            selection = m;
            if (oldSelection != null && oldSelection != m) {
                oldSelection.setSelected(false);
            }
            m.setSelected(true);
        }
    } else if (selection != null) {
        ButtonModel oldSelection = selection;
        selection = null;
        oldSelection.setSelected(false);
    }
}

基本上,现在支持取消​​选择当前元素

public class UnselectableButtonGroup extends ButtonGroup {

    // the list of buttons participating in this group
    protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();

    /**
     * The current selection.
     */
    ButtonModel selection = null;

    /**
     * Creates a new <code>ButtonGroup</code>.
     */
    public UnselectableButtonGroup() {
    }

    /**
     * Adds the button to the group.
     *
     * @param b the button to be added
     */
    public void add(AbstractButton b) {
        if (b == null) {
            return;
        }
        buttons.addElement(b);

        if (b.isSelected()) {
            if (selection == null) {
                selection = b.getModel();
            } else {
                b.setSelected(false);
            }
        }

        b.getModel().setGroup(this);
    }

    /**
     * Removes the button from the group.
     *
     * @param b the button to be removed
     */
    public void remove(AbstractButton b) {
        if (b == null) {
            return;
        }
        buttons.removeElement(b);
        if (b.getModel() == selection) {
            selection = null;
        }
        b.getModel().setGroup(null);
    }

    /**
     * Clears the selection such that none of the buttons in the
     * <code>ButtonGroup</code> are selected.
     *
     * @since 1.6
     */
    public void clearSelection() {
        if (selection != null) {
            ButtonModel oldSelection = selection;
            selection = null;
            oldSelection.setSelected(false);
        }
    }

    /**
     * Returns all the buttons that are participating in this group.
     *
     * @return an <code>Enumeration</code> of the buttons in this group
     */
    public Enumeration<AbstractButton> getElements() {
        return buttons.elements();
    }

    /**
     * Returns the model of the selected button.
     *
     * @return the selected button model
     */
    public ButtonModel getSelection() {
        return selection;
    }

    /**
     * Sets the selected value for the <code>ButtonModel</code>. Only one
     * button in the group may be selected at a time.
     *
     * @param m the <code>ButtonModel</code>
     * @param b <code>true</code> if this button is to be selected,
     * otherwise <code>false</code>
     */
    public void setSelected(ButtonModel m, boolean b) {
        if (b) {
            if (m != selection) {
                ButtonModel oldSelection = selection;
                selection = m;
                if (oldSelection != null && oldSelection != m) {
                    oldSelection.setSelected(false);
                }
                m.setSelected(true);
            }
        } else if (selection != null) {
            ButtonModel oldSelection = selection;
            selection = null;
            oldSelection.setSelected(false);
        }
    }

    /**
     * Returns whether a <code>ButtonModel</code> is selected.
     *
     * @return <code>true</code> if the button is selected, otherwise
     * returns <code>false</code>
     */
    public boolean isSelected(ButtonModel m) {
        return (m == selection);
    }

    /**
     * Returns the number of buttons in the group.
     *
     * @return the button count
     * @since 1.3
     */
    public int getButtonCount() {
        if (buttons == null) {
            return 0;
        } else {
            return buttons.size();
        }
    }

}

我还研究了在按钮上使用ItemListenerChangeListener(以及两者)来尝试控制ButtonModel 的状态

我也尝试了Select Button Group,但它对我不起作用(我没有花任何时间尝试调试它,但它可能仍然对其他人有用)

可运行示例...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            UnselectableButtonGroup bg = new UnselectableButtonGroup();
            JRadioButton buttons[] = new JRadioButton[5];
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            for (int index = 0; index < buttons.length; index++) {
                buttons[index] = new JRadioButton("Button " + index);
                add(buttons[index], gbc);
                bg.add(buttons[index]);
            }
        }

    }

    public class UnselectableButtonGroup extends ButtonGroup {

        // the list of buttons participating in this group
        protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();

        /**
         * The current selection.
         */
        ButtonModel selection = null;

        /**
         * Creates a new <code>ButtonGroup</code>.
         */
        public UnselectableButtonGroup() {
        }

        /**
         * Adds the button to the group.
         *
         * @param b the button to be added
         */
        public void add(AbstractButton b) {
            if (b == null) {
                return;
            }
            buttons.addElement(b);

            if (b.isSelected()) {
                if (selection == null) {
                    selection = b.getModel();
                } else {
                    b.setSelected(false);
                }
            }

            b.getModel().setGroup(this);
        }

        /**
         * Removes the button from the group.
         *
         * @param b the button to be removed
         */
        public void remove(AbstractButton b) {
            if (b == null) {
                return;
            }
            buttons.removeElement(b);
            if (b.getModel() == selection) {
                selection = null;
            }
            b.getModel().setGroup(null);
        }

        /**
         * Clears the selection such that none of the buttons in the
         * <code>ButtonGroup</code> are selected.
         *
         * @since 1.6
         */
        public void clearSelection() {
            if (selection != null) {
                ButtonModel oldSelection = selection;
                selection = null;
                oldSelection.setSelected(false);
            }
        }

        /**
         * Returns all the buttons that are participating in this group.
         *
         * @return an <code>Enumeration</code> of the buttons in this group
         */
        public Enumeration<AbstractButton> getElements() {
            return buttons.elements();
        }

        /**
         * Returns the model of the selected button.
         *
         * @return the selected button model
         */
        public ButtonModel getSelection() {
            return selection;
        }

        /**
         * Sets the selected value for the <code>ButtonModel</code>. Only one
         * button in the group may be selected at a time.
         *
         * @param m the <code>ButtonModel</code>
         * @param b <code>true</code> if this button is to be selected,
         * otherwise <code>false</code>
         */
        public void setSelected(ButtonModel m, boolean b) {
            if (b) {
                if (m != selection) {
                    ButtonModel oldSelection = selection;
                    selection = m;
                    if (oldSelection != null && oldSelection != m) {
                        oldSelection.setSelected(false);
                    }
                    m.setSelected(true);
                }
            } else if (selection != null) {
                ButtonModel oldSelection = selection;
                selection = null;
                oldSelection.setSelected(false);
            }
        }

        /**
         * Returns whether a <code>ButtonModel</code> is selected.
         *
         * @return <code>true</code> if the button is selected, otherwise
         * returns <code>false</code>
         */
        public boolean isSelected(ButtonModel m) {
            return (m == selection);
        }

        /**
         * Returns the number of buttons in the group.
         *
         * @return the button count
         * @since 1.3
         */
        public int getButtonCount() {
            if (buttons == null) {
                return 0;
            } else {
                return buttons.size();
            }
        }

    }

}

【讨论】:

  • 那是很多代码,我会试一试并告诉你
  • @P.Pawar 是的,对此并不满意,但我已经到了这样的地步,要么是反射,要么是反射,如果可以的话,我想避免使用反射:/
  • 我找到了一个解决方案,但它并不优雅。我会更新我的问题详情
  • @P.Pawar 我可能遇到的唯一问题是,ButtonGroup 是否独立于按钮更新,但是否满足您的需求
猜你喜欢
  • 2021-09-30
  • 2012-02-21
  • 2011-07-17
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
相关资源
最近更新 更多