5

Suppose that I have a lot of variables defined in my code with names such as this

public javax.swing.JPanel BenderPanel1;
public javax.swing.JPanel BenderPanel2;
public javax.swing.JPanel BenderPanel3;
etc...

So their general type is like this: BenderPanel"NUMBER".

I want to access some of them and set their visibility with .setVisible(false); but the number of those panels which I want to access is user-defined on run time.

Is there any possible way through a library to append a number to the end of each variable in order to access it in a for loop, like this:

for (int i=1; i<=UserInput; i++)
{
     BenderPanel"i".setVisible(false); // Watch this "i" right there.
}

WITHOUT the need to add them on ArrayList first and do it with the obvious way?

Theocharis K.
  • 1,281
  • 1
  • 16
  • 43

5 Answers5

5

You can't create members dynamically in Java (you can access them dynamically via reflection, but there's no need for it here).

Rather than having

public javax.swing.JPanel BenderPanel1;
public javax.swing.JPanel BenderPanel2;
public javax.swing.JPanel BenderPanel3;

have

public javax.swing.JPanel[] BenderPanels;

or

public List<javax.swing.JPanel> BenderPanels;

Then you can loop through them with an enhanced for loop.

for (javax.swing.JPanel panel : BenderPanels) {
    // ...
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

If you really do not want to store your objects in a data structure like e.g. an ArrayList, I would recommend to use the Reflection API.

Especially interesting for you should be the fields.

Btw: According to the Java Naming Conventions, variable names shouldn't start with capital letters.

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • Thanks for the answer. Also thank you for reminding me JavaNamingConventions which I use almost everywhere but GUI design :D – Theocharis K. Jan 18 '13 at 09:36
0

I'll provide you an idea about using Reflection:

public class YourClassContainer extends ... {

public javax.swing.JPanel BenderPanel1;
public javax.swing.JPanel BenderPanel2;
public javax.swing.JPanel BenderPanel3;
....
public javax.swing.JPanel BenderPanelXXX;

....

//to access all of them:

for (Field field : YourClassContainer.class.getFields()) {
    if (field.getName().startsWith("BenderPanel")) {
        ((javax.swing.JPanel)field.get(YourClassContainer.this)).setVisible(false);
    }
}

}
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • Well this certainly looks uglier and more code than `ArrayList`, so I will just go for the `ArrayList`. +1 for the example though. – Theocharis K. Jan 18 '13 at 09:39
-1

Use Reflection in that Way:

YourClasss instance; // this instance has those all JPanel fields
Method setVisible = JPanel.class.getMethod("setVisible", new Class[] {Boolean.class});

List<String> numberOfFieldList = getNumbersOfFieldsToSetInvisible(); 

for (String number : numerOfFieldList) {
  Field benderPanelField = YourClass.getField("BenderPanel" + number);
  Object fieldInYourInstance = benderPanelField.get(instance);
  setVisible.invoke(fieldInYourInstance, Boolean.FALSE);
}

Maybe it will help.

Michał Kupisiński
  • 3,765
  • 1
  • 19
  • 23
-1

Simple example using your setup:

    List<Integer> visibleItems = new ArrayList<Integer>();

    JPanel[] myPanels = new JPanel[]{BenderPanel1, BenderPanel2, BenderPanel3};

    for (int i = 0; i < myPanels .length; i++) {
        myPanels[i].setVisible(false);
        if(visibleItems.contains(i) ){
            myPanels[i].setVisible(true);
        } 
    }
d.raev
  • 9,216
  • 8
  • 58
  • 79