Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Looping object creation

  1. #1
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Looping object creation

    O hai

    So basically, I was working on my soduko-program when I got one of them "eureka-moments" and I thought I should share. I love the cafe so I'm posting it here but it's prolly missplaced so put it where you want it.

    I'm basically using 81 labels which are clickable to make the 9x9 playing-field. Creating 81 labels is hell if you do it manually, not to speak of the fact that it looks like crap in your code when you have 81 listeners, adds and creations of labels. So basically I wanted a way of creating 81 labels in a simple for loop. The problem, obviously, is that if you just do JLabel label = new JLabel(); 81 times your 80 first ones get avaiable for garbage collection since they have no pointers anymore, and you cant do JLabel STRINGX = new JLabel() either (which sucks, gogo make this possible ploz Sun).

    So I'm basically making a hashmap, giving the value of the first button's key the value (label + i ) and then using i++ in the for-loop, so button 1 becomes button1, button 2 becomes button2 etc.

    Right now you're thinking "So why the hell are you here bragging about it?" and theres actually a good answer:
    I've been scouring forums all over for a sollution for this issue and I haven't found jack on it, and from looking around on this forum I can tell many of you are in the same situation I'm in, having to create 50+ objects that just differ extremely little in state and name. Even if you want to create say 2 different kinds of buttons, you can store object names/states in string arrays and use the array elements to set those specific values like:
    name[x] = "name x"; and value[x] = "value x"; ...you prolly get the idea.


    Hope this helps someone as much as it helps me, and is this actually used by anyone except me or are there even better ways of going about this?

    Love
    // Charlie


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Looping object creation

    are there even better ways of going about this?
    Can you post the code? It be Easier to follow than the text given above

  3. #3
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Looping object creation

    Ill get on it asap but Im going away to a friend today to get a few beers and my code is on my other comp. I'll do it tomorrow when I get home.

  4. #4
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Looping object creation

    Like so:

    //Create labels for all 81 elements in panel, store in Hashtable.
            for (int i = 0; i < 81; i++){
                labKey = "label" + i;
                tempLabel = new JLabel("", 0);
                tempLabel.addMouseListener(this);
                labels.put(labKey, tempLabel);
            }
     
            //Add all labels to panel.
            for(int i = 0; i < 81; i++){
                panel.add((JLabel)labels.get("label" + i));
            }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Looping object creation

    Why not do it in one loop?
    //Create labels for all 81 elements in panel, store in Hashtable.
            for (int i = 0; i < 81; i++){
                labKey = "label" + i;
                tempLabel = new JLabel("", 0);
                tempLabel.addMouseListener(this);
                labels.put(labKey, tempLabel);
                panel.add(tempLabel);
            }

  6. #6
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Looping object creation

    Wanted to try out both the put and the get methods of hashtables. Haven't used em all that much before and practice makes perfect so I figured whattheheck . More efficient to add them directly in the first for-loop tho, thx for the tip.

Similar Threads

  1. Component/Widget creation
    By Hellops in forum AWT / Java Swing
    Replies: 0
    Last Post: October 15th, 2009, 10:31 PM
  2. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM
  3. Replies: 2
    Last Post: July 8th, 2009, 06:35 AM
  4. [SOLVED] minesweeper game creation problem
    By kaylors in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 27th, 2009, 04:06 PM
  5. WAR file creation in Eclipse JEE
    By katty in forum Java IDEs
    Replies: 5
    Last Post: May 21st, 2009, 09:45 AM