Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:26:44+00:00 2026-05-25T00:26:44+00:00

I have a set of classes that implement a particular interface and I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:26:45+00:00Added an answer on May 25, 2026 at 12:26 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If have a set of classes that all implement an interface. interface IMyinterface<T> {
I want to resolve in Windsor a series of classes that implement this interface:
I have some classes that all implement the same Java interface, which essentially allows
I have created two classes that implement AuthorizeAttribute . One is used globally, and
I have a set of classes ( MyClass1, MyClass2, MyClass3 ) that implement an
Under a given namespace, I have a set of classes which implement an interface.
If I have some classes that implement the same interface, so they all contain
Currently I have my VaryByCustom functionality implemented in classes that implement an interface IOutputCacheVaryByCustom
I have written a set of classes and interfaces that are implemented in Moose
I'm using ASP.NET MVC RC2. I have a set of classes that were auto-generated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.