My question is, how i can add variables which are initialized later in the program to a array or arraylist with a loop.
Example:
JButton btn1, btn2, btn3;
...
private void createIco(){
btn1 = new JButton(ico1); // ico1 is a ImageIcon-Object
btn2 = new JButton(ico2);
btn3 = new JButton(ico3);
}
// so i have now the instances of the buttons and now i want them store with
// a loop in a array or arraylist.
private void createBtnArray(){
// here a array
JButton[] buti = new JButton[3];
//how i can store them ? which loop is good or is this the wrong way to
//do ?
}
Edited: Thx so far all.
Yes creating an Array and using this array works fine. Also worked fine by doing all by my hand lol. But my point is more like this :
Lets say i have made many Objects in a stupid way like this:
public class Test extends JFrame {
private BufferedImage cImg, cImg1, cImg2 cImg3 ............
private ImageIcon ico1, ico2, ico3, ico4, ...............
private JButton btn1, bnt2, btn3, btn4, ................
...........
public Test(){
initUI();
}
private void initUI(){
// this methods will initialize all objects above
loadImg();
cropImg();
createIcon();
createButton();
...............
createArray1();
}
public static void main(String[] args){
Test start = new Test();
}
Now i know i can remake all again and use arrays instead of single objects and i did it and it worked. But i think that is much double work, because i made them already and thought with a easy loop i could put all in a array or list or what ever and use then only this array-object to get the other objects.
My wish was that : JButton[] btnAll = new JButton[100]; and then get with a loop all instances what i already created before in the program and use createArray1(), even i dotn know where they are exactly initialized. I just know they are made.
And my next step was, i wanted put all this Arrays again in an object-array to get access only form 1 point. Like this: Object[] all = new Object[3]; all[0] = btnAll; all[1] = icoAll; all[2] = imgAll;
So what i finally wanted look like this : JButton test = all[0[2]];
so i can call only a packed version.
PS: yes i will go for array , list and so on tutorial again to see if i find this there