LayoutManager in a SplitPane Panel

Published by Rob on September 20th, 2004 - in Java

Just finished working out which layout managers in a Swing App that I have been building.

I have got a JSplitPane with a tree on the left, and then another panel on the right where stuff gets displayed.

It was a bit of a pain working out what I wanted the panel in the right to have as a LayoutManager. The thing that made it interesting is that the component for the right panel is dynamically decided, and can be either a JLabel, or a JPanel.

After googling, and playing with different layout managers (JLooks, etc), and reading sun’s stuff. I have ended up having a nasty little bit of code that programmatically decides between two different layout managers. This is a bit of a hack, that is there because of the difference in behaviour that I want to have happen.

When I am displaying a JLabel, I want to respect the size of the JLabel, and display it horizontally centered near the top of the page (I use a FlowLayout for this).

When I am displaying a JPanel I want it to fill up the entire right side of the screen (BorderLayout is good for this usecase).

Like I mentioned, this is a bit hacky, but does the job really well. I get the right layout behaviour when I need it (without adding extra Panels). When there is a JLabel, its size is respected, and a JPanel uses all the space available to it. It looks nice on screen. :)

Related posts:

  1. A quick review of the IntelliJ GUI Editor

2 Responses

  1. Sumit says:

    For that matter, both can be JPanels with BorderLayout. In one case, the JPanel has a JLabel, in another case, a JPanel.

  2. Anonymous says:

    “(without adding extra Panels)”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Rob@Rojotek