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

Thread: Layouts

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Layouts

    I want to create a simple JFame though struggling to get to grips with layouts.

    This is what my JFrame looks like:
    NORTH:
    JTextArea JButton
    CENTER:
    JButton JButton JButton JButton JButton

    .................................................. ..........

    .................................................. .........

    .................................................. ...........
    SOUTH:
    JButton JButton JButton


    The problem is the size of CENTER in comparison to NORTH and SOUTH. Also where the dotted lines are, I want to remove and add labels when buttons are pressed though I don't know how to refer to points underneath the buttons in CENTER, I've tried panels within panels and then tried to make the subpanel be positioned SOUTH of superpanel.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,469
    My Mood
    Mellow
    Thanks
    151
    Thanked 608 Times in 595 Posts

    Default Re: Layouts

    How did you get 5 JButtons in the center?

  3. #3
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Layouts

    sorry, forgot to mention, I added the buttons and text area to a panel, so I spilt the whole layout into 3 panels. One is located at NORTH, one at CENTER and one at SOUTH. I tried adding another panel within the CENTER panel to try and put something within dotted areas though didn't work.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,469
    My Mood
    Mellow
    Thanks
    151
    Thanked 608 Times in 595 Posts

    Default Re: Layouts

    So now the JPanel you've added to the center can have its own layout to which you can add other containers and components. It helps to draw out what you think the finished GUI should look like then consider how the components can be grouped into parts that can be arranged to achieve that design with the existing layout options. Don't confine your thinking to North/Center/South but realize there are many other possibilities by varying what North, Center, and South contain. And don't forget East and West. And also remember that you don't even have to start with BorderLayout but can use another.

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

    keepStriving (November 29th, 2013)

  6. #5
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Layouts

    I've drawn out what I want just struggling with how to implement layouts. I'm trying to keep it simple which is why even though gridbag layout is probably the most useful I need to first understand basic layouts.

    How would I refer to the layout of a nested panel with regards to its superpanel? e.g. at the moment I've tried the following though it never worked, the subpanel didn't even show up, though the superPanel did.

    JLabel label = new JLabel("You have selected button");
     subPanel.add(label);
      subPanel.setBackground(Color.BLUE);
      // super panel is in the CENTER, so I want sub panel to be in the SOUTH within CENTER area.
      superPanel.add(subPanel, BorderLayout.SOUTH);

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,469
    My Mood
    Mellow
    Thanks
    151
    Thanked 608 Times in 595 Posts

    Default Re: Layouts

    It seems you may be adding the subPanel after the GUI is already on the screen. Is that correct? If so, are you revalidate()ing the container? Also, adding/removing components on the fly is usually more trouble than it's worth. CardLayout allows swapping multiple layouts or views and is a much easier way to keep track of what is being presented to the user.

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

    keepStriving (November 30th, 2013)

  9. #7
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Layouts

    Is there no way I can reference to see if there is a certain label present and if so remove it rather than needing to update the whole page?

    --- Update ---

    I've used label.setText(), which is ok for now, now need to learn to make full use of CENTER space.

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,469
    My Mood
    Mellow
    Thanks
    151
    Thanked 608 Times in 595 Posts

    Default Re: Layouts

    Reusing components already in the view as you are now doing is a much better approach than swapping one component for another or adding new ones on the fly.
    Is there no way I can reference to see if there is a certain label present . . . ?
    If you need this functionality, you can build it yourself easily enough, but using CardLayout to swap views would be a better design.

  11. #9
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Layouts

    I've used a card layout too , though I need to introducing some form of nested panels I think to get labels to stack vertically on top of each other instead of getting labels going horizontally which is what is happening at present.

  12. #10
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Layouts

    I have two cardLayout panels which act similar to two separate pages with the one in use added to the JFrame, how would I switch between the two, should I have a super cardLayout panel which is added to the JFrame and sorts which child panel is to be displayed or would it be more easier to removeAll from JFrame and add the other cardLayout panel?

    --- Update ---

    managed to sort the previous problem, by using a parent card layout panel, the following was really useful:
    java - Changing Panels using the Card layout - Stack Overflow

  13. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,469
    My Mood
    Mellow
    Thanks
    151
    Thanked 608 Times in 595 Posts

    Default Re: Layouts

    So you've discovered the beauty and utility of CardLayout. I hope it proves to be as appropriate for you as I imagined it.

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

    keepStriving (December 1st, 2013)

Similar Threads

  1. Help w/ hw , help with panels layouts in frame.
    By Lenjaku in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2013, 03:12 PM
  2. Why do layouts never work the way I want them to?
    By sonicjr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 4th, 2012, 09:17 PM
  3. I am having major issues with layouts
    By angryfetus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 6th, 2012, 09:27 PM
  4. Help with Layouts and Panels
    By Zookey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 29th, 2011, 05:48 PM
  5. Positioning elements. Is it possible without layouts?
    By goodguy in forum AWT / Java Swing
    Replies: 6
    Last Post: January 21st, 2011, 01:24 PM