Setting the size of a JPanel in a BorderLayout

I have a simple JPanel (mainPanel) which has a BorderLayout. To the west I have added another JPanel (westPanel) and in the center I have added a JTextField.

On westPanel I want to use the Graphics object to render some lines onto the Panel, which I can do fine.

However, the size of westPanel is much too small for my needs (as it does not contain any other graphical objects). I have tried using setPreferredSize and setMinumumSize but to no avail.

Is there any way of setting a minimum size on westPanel, or do I have to get rid of the BorderLayout and use co-ordinates!!!

Thanks in advance,

Grant

[651 byte] By [grantlittle] at [2007-9-26 3:54:13]
# 1
Hi,Using co-ordinates would be too troublesome, and not flexible when you resizing your window. Perhap you should try using GridBagLayout. I personally find that GridBagLayout is easy to use, provided that you're familiar in setting the GridBagConstraint. cheers.
chongwei at 2007-6-29 12:43:11 > top of Java-index,Archived Forums,Swing...
# 2
Don't bother with GridBagLayout, setPreferredSize should work, post your code so we can take a look.
chuanhaochiu at 2007-6-29 12:43:11 > top of Java-index,Archived Forums,Swing...
# 3

Sorry for the delay. Here is a snippet of what I'm trying to do, can you see why the westPanel is only staying at around 5pixels even though I have set the preferred size!

thanx for and help

Grant

public class MyFrame extends JFrame

{

private WestPanel westPanel=null;

private JPanel mainPanel;

private JTextField f;

public MyFrame()

{

super();

}

public void init()

{

this.getContentPane().setLayout(new BorderLayout());

westPanel = new WestPanel();

mainPanel = new JPanel();

f = new JTextField();

mainPanel.setLayout(new BorderLayout());

this.getContentPane().add(mainPanel, BorderLayout.CENTER);

mainPanel.getContentPane().add(westPanel, BorderLayout.WEST);

mainPanel.getContentPane().add(f, BorderLayout.CENTER);

this.setSize(120, 70);

this.setVisible(true);

}

class WestPanel extends JPanel

{

public WestPanel()

{

super();

this.setPreferredSize(100, 50);

this.setMinimumSize(50, 20);

//other class specific stuff here

}

}

}

grantlittle at 2007-6-29 12:43:11 > top of Java-index,Archived Forums,Swing...
# 4

Oops!! I slightly adapted the actual code. The lines that read:

mainPanel.getContentPane().add(westPanel, BorderLayout.WEST);

mainPanel.getContentPane().add(f, BorderLayout.CENTER);

should read:

mainPanel.add(westPanel, BorderLayout.WEST);

mainPanel.add(f, BorderLayout.CENTER);

Sorry for the mistake

grantlittle at 2007-6-29 12:43:11 > top of Java-index,Archived Forums,Swing...
# 5

This works.

import java.awt.*;

import javax.swing.*;

public class MyFrame extends JFrame

{

private JPanel westPanel;

private JPanel mainPanel;

private JTextField f;

public MyFrame()

{

westPanel = new JPanel();

westPanel.setPreferredSize(new Dimension(100, 50));

mainPanel = new JPanel();

f = new JTextField();

mainPanel.setLayout(new BorderLayout());

mainPanel.add(westPanel, BorderLayout.WEST);

mainPanel.add(f, BorderLayout.CENTER);

getContentPane().add(mainPanel, BorderLayout.CENTER);

setSize(120, 70);

setVisible(true);

}

public static void main(String[] arg)

{

MyFrame frame = new MyFrame();

}

}

chuanhaochiu at 2007-6-29 12:43:11 > top of Java-index,Archived Forums,Swing...
# 6
Thanks chuanhaochiu,It turned out to be my JVM in the end. I tried my piece of code on a different machine and it worked fine. After reinstalling the JVM on the original machine it now also works properly.Thanks for your time though,Grant
grantlittle at 2007-6-29 12:43:11 > top of Java-index,Archived Forums,Swing...