Java swing checkboxes
In Java, is there an easy way I can only allow 3 out of 10 check-boxes at a time to be checked?开发者_开发问答? Then store the values that are checked into an array.
Thanks
Add a listener to each of the checkboxes. Keep a running list of the checkboxes checked.
If a checkbox is checked, add it to the end of the list.
If it's unchecked, remove it from the list.
If the list's size becomes greater than three, uncheck the checkbox at the beginning of the list, and remove it from the list.
No. You have to do it manually. For example, if 3 boxes are marked, you could set the other boxes to
box[i].setEditable (false);
so the user has to switch a marked box off first.
I don't believe there is anything built-in for this. But keeping a simple
Set<JComponent>
of the checked items, and disabling all non checked when the size is 3 should be pretty simple.
Sadly no there is nothing like that for free. Technically even implementing it wouldn't be so easy. Thought the idea sounds sounds cool I might create this component tomorrow.
Anyway let's think what would you need. I think a constructor should look something like.
MaxToggleController(List<JToggleButton> toggleButtons, int maxToSelect)
Use JToggleButton so a user can use other components then only checkboxes.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
/**
* Controller of toggle button selection. User can specify a maximum
* allowed number of toggle buttons that can be selected at a particular time.
* The controller will make sure that up to the defined maximum number
* of buttons can be selected.
* @author Konrad Borowiecki
*/
public class MaxToggleController
{
/** List of toggle buttons to control. */
private List<JToggleButton> toggleButtons;
/** The maximum number of toggle buttons that can be selected at once. */
private int maxToSelect;
/** The number of toggle buttons that is currently selected. */
private int currentlySelected = 0;
public MaxToggleController(List<JToggleButton> toggleButtons, int maxToSelect)
{
this.toggleButtons = toggleButtons;
this.maxToSelect = maxToSelect;
//install the action listener on each toggle button
for(JToggleButton tB : toggleButtons)
tB.addActionListener(toggleButtonAL);
}
private ActionListener toggleButtonAL = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JToggleButton tB = (JToggleButton) e.getSource();
System.out.println("actionPerformed toggleButtonAL; tB.getText()=" + tB.getText()
+ "; tB.isSelected()=" + tB.isSelected());
//if user deselects it then allow for it
if(!tB.isSelected())
{
currentlySelected--;
tB.setSelected(false);//can be removed
}
else//user tries to select it
{
//if max reached do not allow for the tB selection
if(currentlySelected == maxToSelect)
{
//must be deselected as it is already selected
tB.setSelected(false);
}
else
{
currentlySelected++;
tB.setSelected(true);//can be removed
}
}
String textsSeparator = ", ";
String toggleButtonTexts = "{";
for(JToggleButton toggleB : getSelectedToggleButtons())
toggleButtonTexts += toggleB.getText() + textsSeparator;
int idx = toggleButtonTexts.lastIndexOf(textsSeparator);
if(idx != -1)
toggleButtonTexts = toggleButtonTexts.substring(0, idx)
+ toggleButtonTexts.substring(idx + textsSeparator.length(), toggleButtonTexts.length());
toggleButtonTexts += "}";
System.out.println("actionPerformed toggleButtonAL; currentlySelected="
+ currentlySelected + "; getSelectedToggleButtons().size()=" + getSelectedToggleButtons().size()
+ "\n ; toggleButtonTexts=" + toggleButtonTexts);
}
};
public List<JToggleButton> getSelectedToggleButtons()
{
List<JToggleButton> selectedToggleButtons = new ArrayList();
for(JToggleButton tB : toggleButtons)
if(tB.isSelected())
selectedToggleButtons.add(tB);
return selectedToggleButtons;
}
public static void main(String[] args)
{
List<JToggleButton> tBs = new ArrayList<JToggleButton>();
int noOfTBs = 10;
JPanel contentPane = new JPanel();
for(int i = 0; i < noOfTBs; i++)
{
JToggleButton tB = new JToggleButton("Toggle " + i);
tBs.add(tB);
contentPane.add(tB);
}
int maxToSelect = 3;
MaxToggleController tC = new MaxToggleController(tBs, maxToSelect);
JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
I happen to had some spare time so I have implemented my own controller. There is lots of debugging code left which will allow you to observe what is going on there when you run it. Bare in mind this is not perfect. It is a fast draft, and it has some obvious issues here, e.g. programmatic call of setSelected by user for a toggle button.
Below is updated version of my controller which handles JToggleButton selection setting even if it is done in code.
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
/**
* Controller of toggle button selection. User can specify a maximum
* allowed number of toggle buttons that can be selected at a particular time.
* The controller will make sure that up to the defined maximum number
* of buttons can be selected.
* @author Konrad Borowiecki
*/
public class MaxToggleController
{
/** List of toggle buttons to control. */
private List<JToggleButton> toggleButtons;
/** The maximum number of toggle buttons that can be selected at once. */
private int maxToSelect;
/** The list with the buttons which are currently selected. */
private List<JToggleButton> selectedToggleButtons;
public MaxToggleController(List<JToggleButton> toggleButtons, int maxToSelect)
{
this.toggleButtons = toggleButtons;
this.maxToSelect = maxToSelect;
this.selectedToggleButtons = new ArrayList<JToggleButton> ();
//install the action listener on each toggle button
for(JToggleButton tB : toggleButtons)
tB.addItemListener(itemListener);
}
private ItemListener itemListener = new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
JToggleButton tB = (JToggleButton) e.getSource();
System.out.println("itemStateChanged; tB.getText()=" + tB.getText()
+ "; tB.isSelected()=" + tB.isSelected());
//if the tB is inside the selected toggle button list then remove it from there
if(isInSelected(tB))
{
selectedToggleButtons.remove(tB);
//set false only if it is already true
if(tB.isSelected())
tB.setSelected(false);
}
else//otherwise add it if the size is not max
{
if(selectedToggleButtons.size() == maxToSelect)
{
//set false only if it is already true
if(tB.isSelected())
tB.setSelected(false);//deselect the tB if the list has maximum size
}
else
{
selectedToggleButtons.add(tB);
}
}
String textsSeparator = ", ";
String toggleButtonTexts = "{";
for(JToggleButton toggleB : getSelectedToggleButtons())
toggleButtonTexts += toggleB.getText() + textsSeparator;
int idx = toggleButtonTexts.lastIndexOf(textsSeparator);
if(idx != -1)
toggleButtonTexts = toggleButtonTexts.substring(0, idx)
+ toggleButtonTexts.substring(idx + textsSeparator.length(), toggleButtonTexts.length());
toggleButtonTexts += "}";
System.out.println("; selectedToggleButtons.size()=" + selectedToggleButtons.size()
+ "; toggleButtonTexts=" + toggleButtonTexts);
}
};
private boolean isInSelected(JToggleButton tB)
{
return selectedToggleButtons.contains(tB);
}
public List<JToggleButton> getSelectedToggleButtons()
{
return selectedToggleButtons;
}
public static void main(String[] args)
{
List<JToggleButton> tBs = new ArrayList<JToggleButton>();
int noOfTBs = 10;
JPanel contentPane = new JPanel();
for(int i = 0; i < noOfTBs; i++)
{
JToggleButton tB = new //JCheckBox("Check "+i);
JToggleButton("Toggle " + i);
tBs.add(tB);
contentPane.add(tB);
}
int maxToSelect = 3;
MaxToggleController tC = new MaxToggleController(tBs, maxToSelect);
tBs.get(0).setSelected(true);
tBs.get(5).setSelected(true);
tBs.get(6).setSelected(true);
//the Toggle 7 will not be selected
tBs.get(7).setSelected(true);
JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(400, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Enjoy, Boro.
精彩评论