aspose file tools*
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
Author

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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Bartender

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.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
jQuery in Action, 2nd edition
 
subject: Problem of using BorderLayout