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

Thread: Dynamically add a Jpanel to a Jframe

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Dynamically add a Jpanel to a Jframe

    I have a Jpanel with some widgets on it that I created in Netbeans IDE as a new class ApcUps_Panel

    As I detect different APC UPS's sending a UPD broadcast on the network, I want to add one ApcUps_Panel inside a Jframe for each UPS seen on the network.

    I am a beginner to Java.

    In a nutshell how would I do this?

    AcUps_Panel myApcUps_Panel = new ApcUpsPanel();

    will create a new jpanel object right?

    I need to give it a UPS name feild and draw it on the Jframe at the top down such that each Jpanel is added beloow the last one and a vert scroll bar is created when the JFrame reached the limit of the desktop.

    The Jframe should also grow vertically.


  2. #2
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Dynamically add a Jpanel to a Jframe

    Well, in a nutshell it is as simple as:

    frame.add(panel);

    Design your ApcUpsPanel carefully so you can re-use it when adding it to the frame.

    java memory
    Last edited by namhm; December 4th, 2011 at 05:52 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dynamically add a Jpanel to a Jframe

    Will the next panel apear below the first one added?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Dynamically add a Jpanel to a Jframe

    Quote Originally Posted by Closet_Rambo View Post
    Will the next panel apear below the first one added?
    To manage where Components will appear in the user interface, use a LayoutManager. See the following link:
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

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

    Default Re: Dynamically add a Jpanel to a Jframe

    ApcUps_Frame.add does not fly

    The add method does not exist.

    I am still severly confused on the whole OOP concept here.
    I have a class ApcUps_Frame which has the main method and netbeans constructs the gui Jframe.
    Then I added an ApcUps_Panel class and used the wysiwug editor to drop the lables and button and such.

    So now I can dynamically create a new Jpanel extended custom jpanel object ... right?
    within the main method:

    ApcUps_Panel myApcUps_Panel = new ApcUps_Panel();

    ApcUps_Frame was already created by netbeans and made visable.
    so I did:

    ApcUps_Frame.a .... does not allow "add"


    I don't really know what I am doing here.

    How do you decide which clases to create and construct objects?
    I know the syntax but not the wisdom.
    Last edited by Closet_Rambo; October 1st, 2011 at 11:10 AM.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dynamically add a Jpanel to a Jframe

    I have found this post which is related:

    What is not right here? adding JPanel in JFrame

    This seems a great example but there is no talk of why the classes were contructed this way.
    This is what us newbie OOPs JAVA folks need the most and there is no tutorials anywhere I can find ... how do you program in object within the JAVA paradigm?

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Dynamically add a Jpanel to a Jframe

    Quote Originally Posted by Closet_Rambo View Post
    Will the next panel apear below the first one added?
    It depends on which layout manager you are using, as copeg said.

    java memory
    Last edited by namhm; December 4th, 2011 at 05:53 PM.

Similar Threads

  1. how to convert applet to a JPanel or JFrame
    By flipsyde02 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 29th, 2011, 07:40 AM
  2. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM
  3. JPanel in JFrame
    By maele in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2010, 03:12 AM
  4. Funny business with JFrame, JPanel and JLabel
    By JeffC in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2010, 12:26 PM
  5. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 12:40 PM