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 4 of 4

Thread: Adding panels to a central panel.

  1. #1
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Adding panels to a central panel.

    I have to a lot of sub-panels to a larger 17, 17 gridlayout panel. However a lot of the panels I want to add, are the same. I have gotten the code to work, but only when a new instance of the same subpanel is created for every entrance in the main panel.

    Code work below is working as intended, but is very slow.
    			for(int i = 0; i < 17; i++){
    				for(int y = 0; y < 17; y++){
    					if((i - 8 + heroX < 0) || (y - 8 + heroY < 0) || 
    						(i - 8 + heroX >= 100) || (y - 8 + heroY >= 100)){
    			            gfxOOBPanel = new JPanel(); // I want to move this construct.
    			            gfxOOBPanel.setBackground(new Color(0,0,0)); // same as above
    						boardPanel.add(gfxOOBPanel); // this should be the only code
                                                                        // inside the loops
    					}
    					else if{
                                  ... // Similar constructs as above, adding more panels
                              }
    					}
    				}
    			}

    Naturally I want to pull out the object creation out of the loops, so i dont create 17x17 objects everytime the method is run.
    By pulling it out however the resulting main panel consist of only 1 of each type subpanel, instead of 17x17 grid consisting of the same 4 subpanels used several times.

    TLDR: Adding the same object reference to a panel twice, seems to result in only one added component. (HashMap clash maybe)

    Hope the code explains my problem. The whole gui code is to long to insert here.
    Last edited by Johannes; July 4th, 2010 at 03:45 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Adding panels to a central panel.

    Adding the same object reference to a panel twice, seems to result in only one added component
    A component can only have ONE parent.

    Why do you create the 17x17 grid every time you call the method? Can their creation be moved to a method that is only called once?

  3. The Following User Says Thank You to Norm For This Useful Post:

    Johannes (July 4th, 2010)

  4. #3
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Adding panels to a central panel.

    Quote Originally Posted by Norm View Post
    A component can only have ONE parent.

    Why do you create the 17x17 grid every time you call the method? Can their creation be moved to a method that is only called once?
    The 17x17 panels represents part of a gameworld, which is made up of a large 2d array of Tiles.
    So it reads from the large array, if a certain tile holds a Monster or a water or whatever.
    If the tile contains a monster for example, the corresponding space in the 17x17 gui grid inserts an icon with an image.
    Since everytime you move around the gameworld, the gui grid has to be updated the heavy object creation is not feasable, even though it works as intended(to slow though).

    The Graphics/Graphics2D framework is proberly the way to go here, I just havent worked with it before, so was looking for a quickfix, due to not wanting to read up on it.
    With the one parent rule, Im gonna have to it seems. Thanks for looking into it though

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Adding panels to a central panel.

    I don't understand why you need to create the 17x17 grid more than once.
    Once it is created you should be able to change the individual panels as needed.

Similar Threads

  1. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 08:17 AM
  2. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 04:00 PM
  3. data mapping betweeb different collapsible panel
    By varuntcs in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: May 23rd, 2010, 02:33 PM
  4. suggestionbox inside modal panel
    By smackdown90 in forum Web Frameworks
    Replies: 1
    Last Post: April 8th, 2010, 01:16 PM
  5. adding multiple ints into a string
    By straw in forum Java Theory & Questions
    Replies: 1
    Last Post: March 18th, 2010, 06:02 PM