Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Borderlayout() has gaps even though none are wanted  RSS feed

 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am trying to have an equal east and wast panel divided down the middle. But I keep getting a gap between the two. I want no gap.


 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor Detail

BorderLayout

public BorderLayout()

Constructs a new border layout with no gaps between components.


http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html#LINE_START
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to run your code to see what you were talking about but obviously I can't. So, how can you tell that there's a gap between the two components? And how can you tell that it's due to the BorderLayout and not to the components you've put into the layout?

If you posted an SSCCE it might help people to understand your issue, I think.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: if you put a coloured Border around a component, that makes it much easier to tell how much space it's really occupying. Try it and let us know what you find.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have link to the LINE_START variable of the BorderLayout API, why don't you actually use that variable???


Don't use magic variables. We don't know what "West" and "East" mean. The API has a variable for a reason.

Read the Swing tutorial on How to Use a BorderLayout for working demo code.

Download the demo code and use it as the starting point of your class.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross Gerard wrote:I am trying to have an equal east and wast panel divided down the middle.


Just use a GridLayout(1, 2).
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Just use a GridLayout(1, 2).


This is good advice. I would actually do GridLayout(1, 0) but it works out the same.

The reason, probably, that you're seeing a gap with BorderLayout is that BorderLayout tries to lay out its WEST and EAST components at their preferred widths. example: If the preferred widths are 150 pixels each, and the panel is 400 pixels wide, it will put WEST at 0-150* and EAST at 250-400*, leaving a gap between 150-250*.

If you really want to use BorderLayout, then you might do better by putting one of the components in CENTER and the other in either EAST or WEST. Then would be no gaps because BorderLayout does not respect preferred width for the CENTER/NORTH/SOUTH positions. (Similarly, BorderLayout does not respect preferred height for the CENTER/EAST/WEST positions.)

Btw, rather than passing the literal strings "West" and "East" as parameters to the add() method, I would prefer to see you use the constants BorderLayout.WEST and BorderLayout.EAST. But it works out the same.


*plus or minus a pixel or two. From pixels 0 to pixel 150, inclusive, is actually 151 pixels wide but I'm rounding. Also the panel might have a border, etc.

edit: added the "Then would be no gaps..." bit
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!