0

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

  • 1
    Why would you have `btn1`, `btn2`, and `btn3` in the first place? Just use an array (or list) from the outset. – T.J. Crowder Jun 30 '16 at 08:28
  • 1
    Why have the separate variables at all? Why not just have a `JButton[] buttons` field? – Jon Skeet Jun 30 '16 at 08:28
  • you can store normally, `buti[0]=btn1; buti[1]=btn2; buti[2]=btn3;` if you use out of function you use `buti` variable class instance. – Halil İbrahim Oymacı Jun 30 '16 at 08:28
  • 1
    Hint: don't do this. Do not start creating GUI applications when you just started to learn java. You are lacking absolute essential basic skills. That is like: you are trying to learn how to ride the unicycle ... whilst also learning to juggle with 3 balls. That will not work, and mostly lead to a lot of frustration on your side. A better approach: learn the basics, apply them; and then later on, look into the advanced topics like building UI applications with swing. – GhostCat Jun 30 '16 at 08:33

1 Answers1

2

If you have individual variables like btn1, btn2 etc. you can't use a loop (or at least it wouldn't be easy and make much sense). Just add them normally, e.g.:

buti[0] = btn1;
buti[1] = btn2;
buti[2] = btn3;

Or directly initialize the array:

JButton[] buti = new JButton[3]{ btn1, btn2, btn3 };

However, you might also consider only using an array, i.e. remove btn1 etc. and use buti[0] instead. You'd then initialize it like this:

JButton[] buti;

private void createIco(){
  buti = new JButton[3];      

  buti[0] = new JButton(ico1); // ico1  is a ImageIcon-Object    
  buti[1] = new JButton(ico2);
  buti[2] = new JButton(ico3);
}

Or yet better pass an array of the icons too:

private void createIco(ImageIcon... icons ){

  //initialize as many buttons as are available but only up the the number of icons passed
  int l = Math.min( buti.length, icons.length );      

  for( int i = 0; i < l; i++ ) {
    buti[i] = new JButton( icons[i] );   
  }       

  //if there are still buttons missing initialize them without icons
  for( int i = l; i < buti.length; i++ ) {
    buti[i] = new JButton( );   
  }   
}

Edit:

You might also want to think about using a List<JButton> instead of an array (and if you read Vector<JButton> somewhere: don't use it until you understand why it's still available). Here are some advantages of lists over arrays:

  • Lists can grow, i.e. you don't have to define the maximum number of elements right at the start.
  • You can query a list for its size, i.e. check how many items the list currently contains. With an array you'd only know how many "slots" there are but you don't know how many are not null without checking all elements while with lists you could only add non-null elements and then check the size.
  • Adding and removing elements without creating "holes" is easier with lists as the implementation handles that.
  • If you use the interface List<JButton> you can replace the actual implementation based on your needs (i.e. switch from LinkedList to ArrayList and back)
  • You can use lists whenever a Collection is allowed.

One drawback of lists is that they don't work with primitives such as int out-of-the-box but there are libraries that provide IntList etc. to handle that.

Thomas
  • 87,414
  • 12
  • 119
  • 157