Is this for a school assignment or for something non-educational? The class name make it look almost like an assignment.
I can show you how to do it with the JList, but if its for an assignment I can pretty much guarantee its not covered (so of course that is a nice way of saying you can't just use it). It also only uses strings for the data; you would want to use objects so you can store the name and size together and fetch them as necessary (can also be used if the Model is custom implemented off of an extended collection class since you can then use the Collections.min in conjunction with an object of type Comparable). But it would give you the overall idea.
If you are doing this for a class, there is nothing wrong with checking each item individually as you've done. If you are looking for a max value, you simply keep track of what the max is, and replace it as necessary as you check each checkbox item. The override handling of the JList's primary advantage over that is that it will:
1. Combine the display to the value you want to contain within it (as you will be using an object). So you can retrieve it at any time from the model
2. It allows you to simply add to it without needing to modify the underlying code.
Okay, so here's a quick example of that:
PHP Code:
public static void main(String[] argv)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run()
{
JFrame jf = new JFrame();
Vector<String> vOptions = new Vector<String>();
final JList jl = new JList(vOptions);
// This will make it "look" like a checkbox
jl.setCellRenderer(new ListCellRenderer()
{
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
return new JCheckBox(value.toString(), isSelected);
}
});
// This will make it "act" like a checkbox
jl.setSelectionModel(new DefaultListSelectionModel()
{
private static final long serialVersionUID = 673462916111720119L;
private boolean isStarted = false;
@Override
public void setSelectionInterval(int idx0, int idx1)
{
if (!isStarted)
{
if (isSelectedIndex(idx0))
{
super.removeSelectionInterval(idx0, idx1);
}
else
{
super.addSelectionInterval(idx0, idx1);
}
}
isStarted = true;
}
@Override
public void setValueIsAdjusting(boolean isAdjusting)
{
if (!isAdjusting)
{
this.isStarted = false;
}
}
});
// This will simply print out when you select something in the list
jl.addListSelectionListener(new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent e)
{
for (int is : jl.getSelectedIndices())
{
System.out.println(jl.getModel().getElementAt(is) + " is selected.");
}
System.out.println();
}
});
vOptions.add("Anapolon");
vOptions.add("Anavar");
vOptions.add("Decca");
jf.getContentPane().add(jl);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
});
}
When run you should see what appears to be 3x checkboxes. Checking them will send to system.out the values you have checked (so launch from the command line to see it).
I'm not sure if this is for a school assignment, but if it is then I'd say overriding the renderers and using collections (and possibly arrays?) would be out of the question, but only you can confirm if that's the case and if so where you are. Sometimes simplicity does have its place, especially in the learning process (albeit ultimately being way more code; I could add another 20 items to that list at a cost of only 20x more lines, but to do the same with just variables would require the variables + the code to process them).