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.


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

Results 1 to 2 of 2

Thread: Creating and displaying a JPanel inside another JPanel

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Thanked 0 Times in 0 Posts

    Default Creating and displaying a JPanel inside another JPanel

    I am working on a program in which there is a JPanel in the center (and surrounded by JPanels, but they aren't important to this question). I need to bring up additional JPanels inside the center JPanel, at certain times. I don't seem to be able to find any information on how to do this.

    There's lots of info about the different JPanel layouts, and adding JPanels to frames. I've even seen lots of things written about JPanels nested in other Jpanels. However, I'm just looking for the simple syntax to even call a JPanel from within an already existing JPanel, and I don't seem to be able to find it anywhere.

    When I set up the JPanels initially from the main Frame, I use something like this:
    Container c = getContentPane();
    MenuPanel new = new MenuPanel(); //or replace "MenuPanel" with whatever objected I've created that extendJPanel

    However, if I try to use the same syntax in a JPanel, it says "Incompatible type". I'm guessing because I don't need to get a new ContentPane, maybe? But, then how to I get the JPanel to show up?

    A link to a tutorial, or anything that would help, would be great. I've been searching for tutorials, and there are lots out there about JPanels, but none of them (that I've found) actually deal with calling a new JPanel from within an already existing one.

  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    United Kingdom
    My Mood
    Thanked 294 Times in 227 Posts
    Blog Entries

    Default Re: Creating, and displaying a JPanel inside another JPanel

    Hello JayDuck and welcome to the Java Programming Forums

    Could you please post the code you have so far?

    When you say bring JPanels into the center at certain times, couldn't you have them all in the same location and set each one visable when you want it to show?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  2. Replies: 4
    Last Post: May 27th, 2008, 01:20 PM