Question

How do I assign several JLabels to one 2d array index. I know the code I posted is wrong, I've put it there to just illustrate what I want.

private JLabel img1 = new JLabel();
private JLabel img2 = new JLabel();
private JLabel img3 = new JLabel();
private JLabel img4 = new JLabel();
private JLabel img5 = new JLabel();
private JLabel img6 = new JLabel();
private JLabel img7 = new JLabel();
private JLabel img8 = new JLabel();

private JLabel[][] group =  new JLabel[3][];
group[1][1] = new JLabel (img1, img2, img3);
group[1][2] = new JLabel (img1, img4, img5);
Was it helpful?

Solution

You can't assign multiple objects to a single element of an array.

If you want to group them, use a List and assign the list to the 2D element.

Something like:

List<JLabel> list1 = new ArrayList<JLabel>();
list1.add(img1);
list1.add(img2);
list1.add(img3);

List<JLabel> list2 = new ArrayList<JLabel>();
list2.add(img1);
list2.add(img4);
list3.add(img5);

ArrayList<JLabel>[][] group = new ArrayList<JLabel>[3][2];  // you need to specify the size of the 2nd dimension here if you don't initialize it before using...
group[0][0] = list1;   // 0-based indexing. assuming this is what you wanted...
group[0][1] = list2;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top