tags:

views:

42

answers:

2

How to get a sidebar JPanel of a fixed with with Swing. Now I'm trying this:

public SidebarPanel() {
    this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));   
    this.setPreferredSize(new Dimension(200, this.getPreferredSize().height));
    ...

But when I resize the window also the width of the sidebar changes. How to fix this?

A: 

You'll have to rely upon the good old GridBagLayout. A good helper class is the GBC, which will allow you to position elements with ease.

Riduidel
+4  A: 

There is no guarantee in Swing that the preferred size is respected. This depends on the layout manager of the container.

If the container uses a BorderLayout, you can add the SidebarPanel using:

container.add(sidebarPanel, BorderLayout.EAST)

This will respect the preferred width.

Frederik