Results 1 to 4 of 4
  1. #1
    Soulpole is offline Member
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    6
    Rep Power
    0

    Default Initialize JLabel array and fill

    Hey guys,

    I am trying to create an array of JLabel objects and fill them with one line of code.
    However when I try this it is not seeing it as an array but rather a JLabel object.

    Here is my code:
    Java Code:
    JLabel JLabel[] = {new JLabel("Label 1"), new JLabel("Label 2"), new JLabel("Label 3"), new JLabel("Label 4")};
    
    symbol  : method getLength()
    location: class javax.swing.JLabel[]
    	  for (int i = 0; i < JLabel.getLength(); i++) {
    	                            ^
    1 error
    Thanks in advance,
    Nate

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Initialize JLabel array and fill

    Follow coding conventions. Variable (and also method) names start with a lowercase letter.

    Also, never use an existing class name as an identifier. The compiler is interpreting JLabel as the class name javax.swing.JLabel -- not the array variable you declared with the same name.

    And in future, COPY error messages that you need help with. This certainly isn't the error you got.
    Java Code:
    symbol  : method getLength()
    location: class javax.swing.JLabel[]
    db

    edit: Additionally, arrays don't have a getLength() method. They have a length member. You need to go through some tutorials, programming by guesswork is never satisfying.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Soulpole is offline Member
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    6
    Rep Power
    0

    Default Re: Initialize JLabel array and fill

    Quote Originally Posted by DarrylBurke View Post
    Follow coding conventions. Variable (and also method) names start with a lowercase letter.

    Also, never use an existing class name as an identifier. The compiler is interpreting JLabel as the class name javax.swing.JLabel -- not the array variable you declared with the same name.

    And in future, COPY error messages that you need help with. This certainly isn't the error you got.

    db

    edit: Additionally, arrays don't have a getLength() method. They have a length member. You need to go through some tutorials, programming by guesswork is never satisfying.
    Thanks for the response,
    I did change my variable name to a more appropriate variable name. As for the error message you were correct again. When I used myvar.length it let me compile. I am confused however; I was referencing Oracle's website (API's) java.util.Array and I found a getLength() modifier. That is why I was trying to use myvar.getLength(); I thought that returned my integer.

    My question is: Is there any API that I can see the "array.length" and like attributes?

    Thanks again,
    Nate

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Initialize JLabel array and fill

    Is there any API that I can see the "array.length" and like attributes?
    Read the specification:
    Arrays

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Can't fill the array with the object?
    By Clerek in forum New To Java
    Replies: 9
    Last Post: 02-21-2011, 12:31 AM
  2. How to initialize a two dimensional Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:48 PM
  3. How to initialize an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:47 PM
  4. How to initialize array at runtime
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-09-2007, 03:47 PM
  5. Initialize array at runtime
    By javaplus in forum Java Tip
    Replies: 2
    Last Post: 11-09-2007, 11:44 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
  •