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: Managing layout in JFrame

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Managing layout in JFrame

    I have the following class. When I comment out this.add( rightPanel ); leftPanel appears perfectly. When I comment out this.add( leftPanel );, my rightPanel animates perfectly. When I have both, I only see rightPanel.

    How can I manipulate my layout (potentially with a layout manager such as FlowLayout or BoxLayout) to see both objects?
    import javax.swing.*;
    import java.awt.Dimension;
     
    public class MainFrame extends JFrame
    {
      JPanel leftPanel;   // Contains some TJextField, JButton, JLabel objects
      JApplet rightPanel; // Shows an animation
     
      public MainFrame(String name)
      {
        super(name);
     
        leftPanel = new LeftPane(); 
        rightPanel = new RightPane();
     
        // This is a failed attempt to avoid losing my leftPanel
        leftPanel.setMinimumSize( new Dimension( 120, 100) );
     
        this.add( leftPanel );
        this.add( rightPanel );
     
        rightPanel.start();
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,247
    Thanks
    52
    Thanked 2,299 Times in 2,270 Posts

    Default Re: Managing layout in JFrame

    Have you looked at what layout manager is being used? If you don't explicitly set the layout manager, the JFrame class will use the BorderLayout manager which requires that you pass some details on where you want an added component placed. Read the API doc to see how to use it.
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

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

    stewbond (October 26th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,855
    My Mood
    Mellow
    Thanks
    239
    Thanked 722 Times in 708 Posts

    Default Re: Managing layout in JFrame

    JFrame uses BorderLayout by default and adds components to BorderLayout.CENTER if no other location is specified. Adding two items to the default location will show only the last item added. Either use another layout or add the two components to different locations.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    stewbond (October 26th, 2013)

  6. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Managing layout in JFrame

    Interesting, I thought it used FlowLayout by default. I will lookup BorderLayout and figure out how to manipulate it.

    --- Update ---

    Great! Fixed it with:
    this.add( leftPanel , BorderLayout.LINE_START);
    this.add( rightPanel , BorderLayout.CENTER);

    Note using LINE_START and LINE_END in combination didn't work, so I'm not sure if there is default sizing for CENTER which forces extra space in there.

Similar Threads

  1. Managing Http requests
    By maxitis in forum Java Networking
    Replies: 2
    Last Post: December 1st, 2012, 03:03 PM
  2. Help Managing Generated Data
    By PineAppleKing in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 16th, 2011, 04:06 PM
  3. Managing an embedded database
    By jstn455 in forum JDBC & Databases
    Replies: 1
    Last Post: April 24th, 2011, 10:06 AM
  4. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  5. problem in managing layout
    By namreen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 12:52 AM

Tags for this Thread