Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

arraylist values on buttons

Hi ,

I got a problem. I want to display arratlist values in buttons. ie.there are 9 buttons. I want to display the buttons names from that arraylist. suppose in the list I have 2 4 5 6. I want to diaplay on the button 1 the value 2. on button 2 the value 4. & so on..
But I am getting java.lang.ClassCastException in the line button.setLabel( (String) a); .what should i do.

the code is:-

for (int i=0; i<9; i++) {

button = new Button();
this.add(button);
}
ArrayList arraylist = new ArrayList();

while (true) {
Random random = new Random();
int randomno = random.nextInt( 10);

boolean flag = false;
for (int j = 0; j < (arraylist.size()); j++) {
Integer integer =(Integer) arraylist.get(j);
int randomvalue=integer.intValue();
if (randomvalue == randomno) {

flag = true;
}
}
if (!flag) {

arraylist.add(new Integer(randomno));
}

if (arraylist.size() == 10) {
break;
}
}


for( int j=0,i=0; j<arraylist.size(); j++,i++) {
Object a = arraylist.get(j);

System.out.println("a= " +a);
button.setLabel( (String) a);
System.out.println("List " +arraylist.get(j));
}

}
[1662 byte] By [daina] at [2007-11-11 6:59:52]
# 1 Re: arraylist values on buttons
You are storing Integers in the ArrayList, not Strings. Typecast cannot be done from Integer to String as you are doing.
You can just as well do this:

for( int i=0; i<arraylist.size(); i++) {
Integer bNo=(Integer)arrayList.get(i);
button.setLabel(bNo.toString());
}

or just ...

for( int i=0; i<arraylist.size(); i++) {
button.setLabel(arraylist.get(i).toString());
}
sjalle at 2007-11-11 22:39:44 >
# 2 Re: arraylist values on buttons
Hi sjalle,

It is working. but one problem is there. only last element in the list is displaying in the last button. all other 9 buttons are left blank.In the console all the elements are printing. Can u tell me why.

for (int i=0; i<9; i++) {

button = new Button();
this.add(button);
button.addActionListener(this);
}
for( int i=0; i<arraylist.size(); i++) {
Integer bNo=(Integer)arraylist.get(i);
button.setLabel(bNo.toString());
}
daina at 2007-11-11 22:40:44 >
# 3 Re: arraylist values on buttons
You are loosing references as you go along. In the forst loop you allocate
9 buttons, add them to the container and hook them up to the listener,
ok so far, - but the only button you have a reference to when you exit that
loop is the last button allocated inside the loop
What happends in the next loop is that you are repeatedly setting the label
for that last button.
You can do this at least two ways.

1: Use an array of buttons:

// allocate an array of button references (all will be null pointers initially)
Button [] buttons=new Button[9];
.
.
for (int i = 0; i < 9; i++) {
buttons[i] = new Button();
this.add(buttons[i]);
buttons[i].addActionListener(this);
}
for (int i = 0; i < arraylist.size(); i++) {
Integer bNo = (Integer) arraylist.get(i);
buttons[i].setLabel(bNo.toString());
}

The plus of the code above is that you will have direct access to all the
buttons after they are made, but its not good code...

2: Do it all in one loop:

// either this way, using a button array:
Button [] buttons=new JButton[arraylist.size()];
.
.
for (int i = 0; i < arraylist.size(); i++) {
buttons[i] = new Button();
Integer bNo = (Integer) arraylist.get(i);
buttons[i].setLabel(bNo.toString());
this.add(buttons[i]);
buttons[i].addActionListener(this);
}

// or this way, no button array:
for (int i = 0; i < arraylist.size(); i++) {
Button button = new Button();
Integer bNo = (Integer) arraylist.get(i);
button.setLabel(bNo.toString());
this.add(button);
button.addActionListener(this);
}
sjalle at 2007-11-11 22:41:41 >
# 4 Re: arraylist values on buttons
Hi sjalle

thanks it worked.thanks a lot
daina at 2007-11-11 22:42:46 >