File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem of using BorderLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem of using BorderLayout" Watch "Problem of using BorderLayout" New topic

Problem of using BorderLayout

Yingie Pitts
Ranch Hand

Joined: Oct 10, 2002
Posts: 37
I use BorderLayout one a InternalFrame to arrange three components: JPanel (North), JTree (West) and JTable (East). My problem is the size and location of the JTable.
I used setPreferredSize() and setLocation() but none of them seems work properly. The size is not what I set and the location changes when I drop the InternalFrame. Have you had any similar experience and want to share?

"To err is human, but to really foul things up you need a computer." Paul Ehrlich.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

BorderLayout ignores many of the preferred/max/min dimensions of the components it manages. As the Javadoc says,

The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.

If you need more control, consider using GridBagLayout instead.

[Jess in Action][AskingGoodQuestions]
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Also, since JTable really works the best in a JScrollPane, some of these methods should be applied to the JScrollPane rather than the JTable. JTable also supplies a method with a really long name that helps a lot when dealing with preferred size issues - setPreferredScrollableViewportSize(). Basically, this method tells the scroll pane what size it needs to be when it contains this table. I usually calculate the Dimension passed into it off of the table's column and row sizes.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Problem of using BorderLayout
It's not a secret anymore!