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

Thread: Reason for swing component overlapping?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reason for swing component overlapping?

    Hello,

    I am trying to make a GUI in Java AWT/Swing. I am having trouble pinpointing why my components overlap and hide each other. I am further paranoid after I grabbed a nice example file online, which worked fine, then made a marginal change and instantly started to get the same issue.

    Here is a neat online example I looked at:
    Java Tips - How to align your components in horizontal or vertical layout

    I then eddited thus:
    // place panels on frame
    c.add( boxes[ 0 ], BorderLayout.NORTH );
    c.add( new JButton("myButton1"), BorderLayout.NORTH);
    c.add( new JButton("myButton2"), BorderLayout.NORTH);
    c.add( boxes[ 1 ], BorderLayout.EAST );
    ...

    Original


    Edited:




    EDIT: of course, right after posting I notice the difference in code. Why must the components go into a "Box"? Shouldn't I be able to simply add components and have stack themselves next to each other in a FlowLayout kind of way?
    Last edited by Furious5k; November 5th, 2011 at 06:24 PM.


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reason for swing component overlapping?

    Okay, when using BorderLayout any Components placed inside the 5 regions will expand to fill in weird and interesting ways. For example, when adding a textfield to WEST region it will accept the width parameter for textfield.setPrefferedSize - which by extension will become the width of that region, but ignore the height parameter, instead filling the entire height of whatever the Container for which you've defined BorderLayout is (or to the point where it meets SOUTH and NORTH regions, presumably).

    Simply have to put in a Panel (or some other container) inside the region and then add the Components to this Container.

Similar Threads

  1. Java Tip Jul 29, 2010 - Swing Console Component
    By helloworld922 in forum Java Swing Tutorials
    Replies: 6
    Last Post: April 16th, 2014, 12:08 AM
  2. Replies: 4
    Last Post: July 5th, 2011, 03:00 AM
  3. Java Tip Jul 29, 2010 - Swing Console Component
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: August 24th, 2010, 06:48 AM
  4. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Swing Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM
  5. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM