Results 1 to 5 of 5
  1. #1
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Java BorderLayout problem

    Hi Guys,

    I have the following problem:

    I have 4 components in my GUI that goes like this in the CENTER of the BORDERLAYOUT:
    1. JLabel
    2. JTextArea
    3. Jlabel
    4. JTextArea

    Everytime i make the GUI window bigger, all these elements spread out. How can i get these elements to have a BREAK line (i.e. a break line after 1. Jlabel) so that no matter how i stretch the window, it will stay in-tact and the other elements will follow underneath.

    Thanks

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Java BorderLayout problem

    In most cases, you need to combine several layouts. This one looks like a JPanel with a GridLayout with four rows, in the BorderLayout.CENTER of a JPanel with a BorderLayout.

    Edit: if you want the JTextAreas to grow when resizing, but not the JLabels, you are looking at a GridBagLayout which is more complex, but more versatile when resizing, spanning etc. If you can use third party libraries, I can recommend MigLayout.
    Last edited by SurfMan; 04-21-2015 at 11:22 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java BorderLayout problem

    Or a BoxLayout. First stop should be the visual index to just see which one might fit the bill the most:

    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java BorderLayout problem

    Also keep in mind that you don't have to put everything into one panel. For example, if you wanted a label and textbox on one line and the same on the next line you could put each in their own JPanel. Then use one layout manger for the panels and perhaps a different one to layout the components within each panel. But I commend you for not using GUI designers or absolute positioning. You are definitely on the right track.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java BorderLayout problem

    For proper aligning I've used the SpringLayout to create that kind of "web form layout". But that layout manager itself is a monster to use manually, the swing tutorial has a neat SpringUtilities class with which you can automatically generate a neat grid with one simple call:

    Java Tutorials Sample Code

    If that direct link doesn't work:

    Laying Out Components Within a Container: Examples (The Java™ Tutorials > Creating a GUI with Swing > Laying Out Components Within a Container)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. BorderLayout
    By Madoka in forum New To Java
    Replies: 1
    Last Post: 04-17-2012, 07:12 AM
  2. How to use BorderLayout
    By Daman12 in forum New To Java
    Replies: 15
    Last Post: 10-27-2011, 05:30 AM
  3. BorderLayout problem
    By TGH in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 09:51 PM
  4. BorderLayout
    By oneself in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 10:59 PM
  5. Help with BorderLayout
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 07:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •