views:

30

answers:

2

I want to create a JLabel (containing an image) in the north position of a border layout that auto-sizes to a length matching the preferred width of a component in the center position of a border layout.

The only way I can do this at present is to create another panel in the north position and add the label in the center position of this panel.

Is there a way to do this without the extra panel?

A: 

Well, I'm not sure I understand the question. A JLabel does not "autosize" itself. The size of the label is the size of the Icon added to the label. So the size of the image will not change even if the width changes.

Maybe you can use:

label.setAlignmentX(...);
label.setHorizontalAligment(...);

To horizontally center the label in the North of the panel, if thats what your question is.

Why don't you post your currently working SSCCE that shows what you are doing. Also, what is the problem with using a second panel?

camickr
+1  A: 

There is no need to add extra panel, As I see that you only need Label in North (i.e. Top). Components added to north in borderlayout will occupy complete width and height will be preffered height of component. This is decided on various factors.

You just need to take care of setting label text and image in center. Look at alignment api's of label for same.

Details: http://www.ehow.com/way_5579409_java-borderlayout-tutorial.html

e.g.

http://www.java2s.com/Tutorial/Java/0240_Swing/1340_BorderLayout.htm

http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/border.html

YoK