取消选择NetBeans中属于buttonGroup的单选按钮

时间:2018-08-15 23:01:34

标签: java user-interface netbeans netbeans-8

我正在制作一个涉及NetBeans中许多单选按钮和buttonGroup的UI。假设将两个单选按钮分配给相同的buttonGroup。一旦单击单选按钮之一,就无法取消选择它。我应该怎么做才能使用户再次单击而取消选择阅读按钮?这是我的尝试:

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变得混乱,有时必须单击两次按钮组中的单选按钮才能再次选择它们。

1 个答案:

答案 0 :(得分:0)

好的,这有点hack。有很多问题很难解决。

ButtonGroup是一个具体的类,因此制作自定义版本很困难(这就是为什么我喜欢interface s),由于它非常依赖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();
            }
        }

    }

}