Results 1 to 5 of 5
  1. #1
    nome is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default create new Objects with for loop

    Hello,
    I wanted to use for loop to create new labels and used this

    for (int i = 0; i < 4; i++) {
    JLabel label[i] = new JLabel("label " + i);
    }

    but I get the error
    Type mismatch: cannot convert from JLabel to JLabel[]

    Any help would be appreciated
    Last edited by nome; 02-27-2012 at 03:19 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: create new Objects with for loop

    Believe the error message as it's correct. It's telling you that this class doesn't have an image(...) method that takes an int for a parameter. So what is this image method? Or do you instead mean to use an array named image?

  3. #3
    nome is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Re: create new Objects with for loop

    Fubarable--I edited the posting since my initial one, I am trying to create four labels using a loop. Thank you.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: create new Objects with for loop

    Quote Originally Posted by nome View Post
    Hello,
    I wanted to use for loop to create new labels and used this

    for (int i = 0; i < 4; i++) {
    JLabel label[i] = new JLabel("label " + i);
    }

    but I get the error
    Type mismatch: cannot convert from JLabel to JLabel[]

    Any help would be appreciated
    Quoting so my answers make sense and stay in sync with your questions --

    @nome: you've got to show more code. I don't know how we can be expected to guess what's wrong based on this limited amount of data. How much code, I don't know as that's where the art of asking a question comes into play. Not too much or no one will take the time to read it, but not a few scant lines either. For instance, where do you declare your array? Is it a 2-dimensional array (which I'm betting it is)?

  5. #5
    nome is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Re: create new Objects with for loop

    @Fubarable
    I solved it. THis is what I was trying to do.

    for (int i = 0; i < 4; i++) {
    JLabel label = new JLabel("label" + i);
    label.setName("label" + (i));
    }

    THanks for your replies

Similar Threads

  1. recursive function to create many objects?
    By dacoolest in forum New To Java
    Replies: 1
    Last Post: 11-22-2011, 09:03 PM
  2. Replies: 0
    Last Post: 03-03-2010, 11:07 AM
  3. For loop to create objects
    By SteroidalPsycho in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 10:31 AM
  4. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 03:14 AM
  5. Replies: 1
    Last Post: 01-22-2009, 05:25 PM

Tags for this Thread

Posting Permissions

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