1

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?

mallardz
  • 1,070
  • 11
  • 21
  • 3
    If you click on the tag `radio-button`, you will see its description which says user can only select exactly one out of the number of choices. It is meant for only one-only selection. Use check boxes. It is not that difficult to limit selection to less than total number of check boxes. – SomeDude May 25 '17 at 12:40
  • may this help https://stackoverflow.com/questions/26156310/how-can-i-limit-the-check-box-selected – Kumar Gaurav Sharma May 25 '17 at 12:44

1 Answers1

4

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();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288