views:

253

answers:

4

i am learning Java at the moment and have the following question:

i am adding my controls to JFrame and then pack() before displaying.

this runs the application and all is very nice.

i was wondering is there a way to stop the user from resizing the application window?

also is there a way to for the image in JLabel to expand as the user changes the application window?

at the moment i have it as:

    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.CENTER;

and it only centers the image, i would like to be able to expand/shrink the image.

thanks.

+2  A: 

i was wondering is there a way to stop the user from resizing the application window?

If you don't want to allow users to resize your window at all then you can do

frame.setResizable (false);



I'm not sure that this technique can be successfully used at the same time with pack(). You should certainly try, but I remember there was an issue that if you do:

frame.setResizable (false);
pack();

then pack() simply doesn't do what it should and if you do:

pack();
frame.setResizable(false);

then you window becomes a little more narrow then after only pack(). Of course, some workaround can be found (or maybe even it's just my bad experience).

Roman
this.setResizable(false); pack(); this worked. the other way it produced a different result by introducing some padding around the controls.
iEisenhower
@ikurtz: that's nice! I tried it with java 1.4, maybe something changed since then.
Roman
thanks for the help. im using Java 1.6.0_17 and NetBeans 6.8. glad it is working!
iEisenhower
+2  A: 

for the first question, you can stop the user from resizing your window using the method setResizable(boolean):

frame.setResizable(false);

I don't use GridBagLayout, so I can't help you with the second question.

cd1
The second question is not related to the use of GridBagLayout, but how to scale the image in a JLabel.
jarnbjo
+1  A: 

To prevent resizing the window, you can simply call setResizable(false) on your Window or (J)Frame.

As to the JLabel and image, there is no easy way to make it scale the image according to it's actual size. If you use the label just for the image (no text), it's probably easier for you to implement your own component, in which you paint the image yourself.

jarnbjo
+1  A: 
also is there a way to for the image in JLabel to expand as the user changes the application window?

Yes you need to create your own LabelUI class and set it to your label via setUI(). In your custom LabelUI class, you will have to override getPreferredSize and paint the imageIcon as you wish.

It is slightly programmatic, you may want to look at source of BasicLabelUI to understand how things are done.( http://www.java2s.com/Open-Source/Java-Document/Apache-Harmony-Java-SE/javax-package/javax/swing/plaf/basic/BasicLabelUI.java.htm )

or

you could over-ride paintComponent() API of JLabel by creating your own extended JLabel class.

ring bearer