I am trying to call a Label by integer number. Such as if N = 1, it will return label, if N = 2, it will return label2. I have tried with Map but I can't think properly.
Map<Integer, String> box = new HashMap();
{
box.put(1, "label");
box.put(2, "label2");
}
JLabel label = new JLabel();
ImageIcon image1 = new ImageIcon("BlackKnight.png");
label.setIcon(image1);
JLabel label2 = new JLabel();
ImageIcon image2 = new ImageIcon("BlackBishop.png");
label2.setIcon(image2);
Trying to give a pseudo code:
input N
output Nth Label name
If you just want your label's name, you can simply call: box.get(1)
to get the name of label1, or box.get(n)
for the n'th label.
If you want to actually get the entire JLabel, you can change the map to: Map<Integer, JLabel> box = new HashMap();
and use box.put(1, label);
and box.put(5, label5);
etc which you can later call back using the same box.get(n)
method described above.
In the latter case, you'll have to create the labels first before putting them inside the map though