Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Arraylist values on buttons

843807Oct 7 2005 — edited Oct 7 2005
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));
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2005
Added on Oct 7 2005
3 comments
479 views