Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: arraylist values on buttons

  1. #1
    Join Date
    Sep 2005
    Posts
    10
    Rep Power
    0

    Default 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));
    }

    }

  2. #2
    Join Date
    Oct 2005
    Posts
    310
    Rep Power
    0

    Default

    Change this line: button.setLabel( (String) a);
    to: button.setLabel(a.toString());

    The problem is casting a which is of type Object to type String.
    Yesterday is history, tomorrow is a mystery, but today is a gift ... that is why it is called the present

  3. #3
    Join Date
    Jun 2003
    Posts
    453
    Rep Power
    0

    Default

    try this and let me know if it works; instead of

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

    try this:

    Integer a = (Integer)arraylist.get(j); /**Since you already know the type of object - an Integer*/
    System.out.println("a= " +a);
    button.setLabel( a.toString() ); /**Returns a String object representing this Integer's value.*/

  4. #4
    Join Date
    Jun 2003
    Posts
    453
    Rep Power
    0

    Default

    hey hirky, you first me

  5. #5
    Join Date
    May 2005
    Posts
    59
    Rep Power
    0

    Default

    I'm just curious, why did u put the variable 'i' in your last loop?
    for( int j=0,i=0; j<arraylist.size(); j++,i++
    "I don't believe in astrology; I'm a Capricorn and we're skeptical."

  6. #6
    Join Date
    Sep 2005
    Posts
    10
    Rep Power
    0

    Default

    Hi Billerg

    Thanks for the help. Its working fine now. Thanks again
    Last edited by d22; Oct 10, 2005 at 12:04 AM.

  7. #7
    Join Date
    Jun 2003
    Posts
    453
    Rep Power
    0

    Default

    the reason that happens is that you need to create an array of buttons in order to set the title to each one or do everything in the first "for loop". So what you might end up with is this:

    /**
    * this is how you declare the button array:
    *
    * Java.AWT.Button button = new Java.AWT.Button [9];
    * Using the 9 array of buttons the code should now read:
    */


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

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



    /**
    * also in the first for loop add change the button initialization to the following
    **/
    button[i] = new Button();

    Oh by the way, the code can be cleaned up better (less lines etc) but first get the basics correct.

  8. #8
    Join Date
    Jun 2003
    Posts
    453
    Rep Power
    0

    Default

    @jagfun, is that Mr. Unreal Tournament?

  9. #9
    Join Date
    May 2005
    Posts
    59
    Rep Power
    0

    Default Rework the code

    I think u r only adding text to 1 button. I'll bet it's the last button created that has any text. U need to create an array for ur buttons and add the text u want while traversing the array. I would also suggest using an ordinary integer array instead of an ArrayList just to reduce ur overhead since u already know the number of elements u need. Also u only need one instance of Random so create it outside of the while loop. If u use my suggestions you will have rework ur random number generator code a bit to accomodate the integer array.

    Button[] button = new Button[9];
    Random random = new Random();
    for (int i=0; i<button.length ; i++) {
    button[i] = new Button();
    this.add(button[i]);
    int[] a=new int[button.length];
    .
    .//random number generator code
    .
    button[i].setLabel( ""+ a[i]);
    }
    "I don't believe in astrology; I'm a Capricorn and we're skeptical."

  10. #10
    Join Date
    May 2005
    Posts
    59
    Rep Power
    0

    Default

    yeah I used to be called Mr. Unreal Tournament when I lived in Mobay.
    "I don't believe in astrology; I'm a Capricorn and we're skeptical."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •