-
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));
}
}
-
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:
Code:
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());
}
Last edited by sjalle; 10-07-2005 at 11:06 AM.
eschew obfuscation
-
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());
}
-
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:
Code:
// 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:
Code:
// 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);
}
eschew obfuscation
-
Hi sjalle
thanks it worked.thanks a lot
Similar Threads
-
By Gengar003 in forum Java
Replies: 10
Last Post: 05-15-2005, 06:14 AM
-
By Shaitan00 in forum Java
Replies: 4
Last Post: 04-13-2005, 09:37 PM
-
By Jason Salas in forum ASP.NET
Replies: 4
Last Post: 07-31-2003, 08:40 PM
-
Replies: 1
Last Post: 06-19-2002, 07:30 AM
-
By Paul Klanderud in forum ASP.NET
Replies: 2
Last Post: 07-26-2000, 10:13 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|