Results 1 to 5 of 5
  1. #1
    rvd1ofakind is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Making an array of existing JLabels.

    so i have JLabels, named SO0, SO1, SO2, SO3... i need to make them go into an array.
    but this doesn't work: public JLabel[] SO = {SO0, SO1, SO2....};
    My teacher said to use a for loop. But how can i use it? I can't do this:

    for (i=0;i<26;i++){
    SO[i]=SO+i);
    }

    Is there a way to do something like SO[i] = nameOfJLabel("SO+i");
    i know this doesn't work, it's just for you to get the point of my problem.
    I really don't want wo write
    SO[0]=SO0;
    SO[1]=SO1;
    SO[2]=SO2;
    SO[3]=SO3;

    since the other 3 arrays i need to create have 76 elements =/

  2. #2
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Thumbs up Making an array of existing JLabels.

    This will help to you for declaring array.


    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)




    mcajavaprogramer

  3. #3
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: Making an array of existing JLabels.

    no there isn't a way to have the code create variable names for you but you can use their position in the array as a sort of variable name so when your using the label instead of using SO1.getText(); you can use SO[1].getText();.
    Now you can have the text of the labels generated by the code like so:
    hope this helps
    Java Code:
    		JLabel [] labels = new JLabel[10];
    		String labelText = "";
    		for(int i=0; i <10; i++)
    		{
    			labelText = "SO"+i;
    			labels[i] = new JLabel(labelText);
    		}

  4. #4
    watachiaieto is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Lightbulb Re: Making an array of existing JLabels.

    Quote Originally Posted by rvd1ofakind View Post
    so i have JLabels, named SO0, SO1, SO2, SO3... i need to make them go into an array.
    but this doesn't work: public JLabel[] SO = {SO0, SO1, SO2....};
    My teacher said to use a for loop. But how can i use it? I can't do this:

    for (i=0;i<26;i++){
    SO[i]=SO+i);
    }

    Is there a way to do something like SO[i] = nameOfJLabel("SO+i");
    i know this doesn't work, it's just for you to get the point of my problem.
    I really don't want wo write
    SO[0]=SO0;
    SO[1]=SO1;
    SO[2]=SO2;
    SO[3]=SO3;

    since the other 3 arrays i need to create have 76 elements =/
    you were close.
    This will compile them into an array of JLabels:

    public JLabel[] SO = new JLabel[] {SO0,SO1,SO2...);

    About the second part, I am slightly confused. we would need a little more code to understand what you want accomplished...

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Making an array of existing JLabels.

    Quote Originally Posted by watachiaieto View Post
    you were close.
    This will compile them into an array of JLabels:

    public JLabel[] SO = new JLabel[] {SO0,SO1,SO2...);
    Not required. The snippet originally posted:
    Java Code:
    JLabel[] SO = {SO0,SO1,SO2...);
    is valid, provided that the variables SO0, SO1, SO2 ... have already been initialized otherwise you end up with a JLabel array of null references.

    Any future (re)assignment of the individual variable references won't automagically update the value contained in the array.

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

Similar Threads

  1. making a gamefield (without using a double array)
    By liluma in forum New To Java
    Replies: 0
    Last Post: 10-05-2011, 06:40 PM
  2. Need some help with JLabels.
    By Medabi3 in forum Java Applets
    Replies: 1
    Last Post: 08-24-2011, 08:28 PM
  3. Replies: 1
    Last Post: 06-24-2011, 07:36 AM
  4. Replies: 12
    Last Post: 12-12-2010, 07:04 PM
  5. Making a String Array from a text file
    By MrJimmah in forum New To Java
    Replies: 5
    Last Post: 11-17-2010, 12:19 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
  •