Casting numbers to variable names

I'm not sure if casting is the right term in here but here is what I'm trying to figure out how to do...

What I’m doing right now:
    private void setBoxValue (int location, boolean value) // location can be a String it doesn’t really matter
    {
          switch(location) {
          case 0:
                txtMaxTries_0.setEnabled(value);
                break;
          case 1:
                txtMaxTries_1.setEnabled(value);
                break;
          case 2:
                txtMaxTries_2.setEnabled(value);
                break;
                // and so on
          }

    }

What I would like to be able to do:
    private void setBoxValue (int location, boolean value) // location can be a String it doesn’t really matter
    {
          txtMaxTries_(location).setEnabled(value);
    }


Thanks for your help,
Kasia
kasiencjaAsked:
Who is Participating?
 
CEHJCommented:
You need to keep an array of the component, then

 txtMaxTries[location].setEnabled(value);
0
 
objectsCommented:
you could use a Map with a String (or any class) location:

    private Map txtMaxTries = new HashMap();

    private void setBoxValue (String location, boolean value) // location can be a String it doesn’t really matter
    {
         JCheckBox txtMaxTry = (JCheckBox) txtMaxTries.get(location);  // assuming they are checkboxes, change type if not
         txtMaxTry.setEnabled(value);
    }
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.