Product Development in Brisbane

LayoutManager in a SplitPane Panel

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. :)

2 Responses to “LayoutManager in a SplitPane Panel”

  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