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: Trying to add text from a string to the name of a JPanel (is it possible?)

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to add text from a string as the name of a JPanel (is it possible?)

    I am trying to set the size of some of my panels, and since they will all be the same size i thought i could do it using a for loop, in which it loops through the different panels in the array and sets their size.

    for example when it is at index 0 then it should have the name of the first panel
    i want to set the size for, however i'm not sure how to then get it to extract the word from the variable rather than use the variables name itself as the word?
    If you get what Im trying to do here:

    	        String[] array = {"pepperoniPanel", "mozarellaPanel", "hawaiianPanel", "meatXtremePanel"};
    	        for (int i = 0; i < array.length; i++)
    	        {
    	        	String current;
    	        	current = array[i];
     
    	        	current.setMaximumSize(new Dimension(sizeX, sizeY));
    	        	current.setPreferredSize(new Dimension(sizeX, sizeY));
     
    	        }
    Last edited by emkoirl; April 7th, 2011 at 10:57 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,332
    My Mood
    Hungover
    Thanks
    143
    Thanked 624 Times in 535 Posts

    Default Re: Trying to add text from a string to the name of a JPanel (is it possible?)

    You can't really do that. You could have a Map of variable names to JPanels, though. Or you could just add them all to a List.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to add text from a string to the name of a JPanel (is it possible?)

    Ah too bad, thanks for the reply

    I will just add them all to the list, i was thinking perhaps there was an easy yet more efficient and quick way to set the size of the JPanels, rather than having a long list of the same code repeated over and over just with different panels.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,332
    My Mood
    Hungover
    Thanks
    143
    Thanked 624 Times in 535 Posts

    Default Re: Trying to add text from a string to the name of a JPanel (is it possible?)

    What? No, if you add them to a List, you can just iterate over the List instead of iterating over an array of variable names. You shouldn't have any repeated code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to add text from a string to the name of a JPanel (is it possible?)

    Oh sorry I guess i got it wrong then, I'm a bit new to this. Do you mean to add them to a JList? how exactly would I iterate that to set the size for each of the items inside the list?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,332
    My Mood
    Hungover
    Thanks
    143
    Thanked 624 Times in 535 Posts

    Default Re: Trying to add text from a string to the name of a JPanel (is it possible?)

    Nope. I mean add them to a List, such as an ArrayList.

    Recommended reading: Trail: Collections (The Java™ Tutorials)

    You could also just have an array of JPanels instead of storing their names in an array. Wouldn't that save you a step?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. making text editor using string class
    By petadeer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2011, 02:28 AM
  2. Text File into String Array
    By mathanv in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 04:30 AM
  3. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 04:31 PM
  4. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 10:56 AM
  5. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM