2

I have a set of classes that implement a particular interface and I have a set of checkboxes. I want to throw an error if no checkboxes are selected. If atleast one or more checkboxes are selected, then it should create objects associated with that checkbox.

This is how I done.

interface U { ... }

class A implements U { ... }
class B implements U { ... }
class C implements U { ... }

class Main {
    //.... 
    //....
    public void findSelectedCheckBoxesAndCreateObjects() {
        if(!(checkboxA.isSelected() || checkboxB.isSelected() || checkboxC.isSelected()) {
            System.out.println("No checkboxes selected");
            return;
        }

        //if any selected, create associated object
        if(checkboxA.isSelected()) new A(file);
        if(checkboxB.isSelected()) new B(file);
        if(checkboxC.isSelected()) new C(file);
    }
}

Now I have 3 problems.

  1. This is just a sample code. Original has 8 checkboxes and classes with more coming.
  2. I can't keep adding || checkboxD.isSelected() every time I have a new class for checking it.
  3. Same thing. I can't keep adding if(checkboxD.isSelected()) new D(file); for every class.

It is very inelegant. Can I have some kind of loop that removes the redundant code?

Please give me your suggestions. Thank you.

Vigneshwaran
  • 3,265
  • 6
  • 23
  • 36
  • 1
    questions, 1) is there possible to select only one JCheckBox, or two, three ... more than once, 2) `public void findSelectedCheckBoxesAndCreateObjects() {` will be called from JCheckBox#ItemListener or ActionListenere or there is/are for that for example Action from JButton ??? any Event/Action – mKorbel Aug 26 '11 at 15:44
  • answers: 1. No. More than one checkbox can be selected. 2. It's an example action from JButton. :) – Vigneshwaran Aug 26 '11 at 16:54
  • for proof code you have to look write larger workaroud for ButtonGroup if you awaiting/expects correct output in all cases http://download.oracle.com/javase/tutorial/uiswing/components/buttongroup.html and best around by DB http://tips4java.wordpress.com/2008/11/09/select-button-group/, but Map or Set should be fine for that too – mKorbel Aug 26 '11 at 17:47

1 Answers1

5

You should use a collection structure to hold your checkboxes and those related classes. Using a Map you could do something like this:

Map <JCheckBox,Class<U>> uCheck = new HashMap<JCheckBox,Class<U>>();

// add your checkboxes and U-classes to the map

uCheck.put(checkBoxA, A.class);

Now, it's quite easy to get a collection of the classes that need to be instantiated based on the checkbox status:

public Collection<Class<U>>  getEnabledClasses(<JCheckBox,Class<U>> checkMap) {
    List<Class<U>> result = new LinkedList<Class<U>>();
    for (Map.Entry<JCheckBox,Class<U>> entry:checkMap.entrySet()) {
        if (entry.getKey().isSelected()) {
            result.add(entry.getValue());
        }
    }
}

Now, a call to getEnabledUs(uCheck) returns a collection of the selected classes. If the collection is empty, there's no selection, hence nothing to do.

for (Class<U> u:getEnabledClasses(...)) {
    Constructor<U> cons = u.getConstructor(...);
    U instance = cons.newInstance(fileparameter);
    instance.doSomething(...);
}

That should get you started. (*) Disclaimer: this is non-tested code. Rather pseudo-code with crisp detail only where needed.

maasg
  • 37,100
  • 11
  • 88
  • 115
  • Awesome!! Thank you very much. I had to use Class extends U> for my case and used ArrayList instead of LinkedList for performance reasons. Other than these, everything else as you put. Code looks great now. – Vigneshwaran Aug 27 '11 at 07:07
  • Sorry for the grammar mistake in question. I meant to type "Help me to avoid.." :P – Vigneshwaran Aug 27 '11 at 07:08