How to limit selection to a maximum of two out of three radio buttons in Swing?

99封情书 提交于 2021-02-07 10:31:27

问题


Adding radio buttons to a button group limits the user to selecting any one radio button (or none).

Is there a way of limiting the user to selecting up to n radio buttons, where n is more than 1 but less than the total number of buttons?

In my current case, I am converting a 2D point on a drawing into the theoretical 3D space that the 2D drawing describes. Any one 2D point might represent multiple points in 3D space, so the user has the option to constrain the 3D point by specifying the value of the 3D point in a maximum of 2 dimensions. Obviously all 3 dimensions does not make sense. The dimensions to be constrained are selected using radio buttons.

Does Swing allow for an easy way to implement this?


回答1:


Don't use radio buttons. Radio buttons are used when you only want 1 from a group.

Instead use check boxes. Check boxes are used when you want 0 or more.

Here is a example that allows you to create a "group of check boxes" so you can limit the maximum number of selections:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class CheckBoxGroup
{
    private Set<GroupButtonModel> models = new HashSet<GroupButtonModel>();
    private int groupSize;

    public CheckBoxGroup(int groupSize)
    {
        this.groupSize = groupSize;
    }

    public void register(JCheckBox checkBox)
    {
        ButtonModel groupModel = new GroupButtonModel();
        groupModel.setSelected ( checkBox.getModel().isSelected() );
        checkBox.setModel( groupModel );
    }


    private class GroupButtonModel extends JToggleButton.ToggleButtonModel
    {
        @Override
        public void setSelected(boolean selected)
        {
            if (!selected)
            {
                models.remove( this );
                super.setSelected( selected );
                return;
            }

            //  Check number of currently selected check boxes

            if (models.size() == groupSize)
            {
                System.out.println("Only " + groupSize + " items can be selected");
            }
            else
            {
                models.add( this );
                super.setSelected( selected );
            }

        }
    }

    private static void createAndShowGUI()
    {
        JPanel panel = new JPanel();
        CheckBoxGroup group = new CheckBoxGroup(3);

        for (int i = 0; i < 10; i++)
        {
            JCheckBox checkBox = new JCheckBox( String.valueOf(i) );
            panel.add( checkBox );
            group.register( checkBox );
        }

        JFrame frame = new JFrame("Check Box Group");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/44180720/how-to-limit-selection-to-a-maximum-of-two-out-of-three-radio-buttons-in-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!