arraylist values on buttons


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: arraylist values on buttons

  1. #1
    Join Date
    Sep 2005
    Posts
    22

    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
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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 12:06 PM.
    eschew obfuscation

  3. #3
    Join Date
    Sep 2005
    Posts
    22
    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());
    }

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  5. #5
    Join Date
    Sep 2005
    Posts
    22
    Hi sjalle

    thanks it worked.thanks a lot

Similar Threads

  1. Replies: 10
    Last Post: 05-15-2005, 07:14 AM
  2. Replies: 4
    Last Post: 04-13-2005, 10:37 PM
  3. Replies: 4
    Last Post: 07-31-2003, 09:40 PM
  4. Replies: 1
    Last Post: 06-19-2002, 08:30 AM
  5. losing Form values from option boxes
    By Paul Klanderud in forum ASP.NET
    Replies: 2
    Last Post: 07-26-2000, 11: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center