Skip to main content
gamedev.net
🔒 Locked

[java] min width / preferred width

Started by kVandaele 1 reply 750+ views
Original Post
kVandaele
kVandaele
using Java 1.5.0_update6 basic setup: main window: JApplet: getContentPane().setLayout( new BorderLayout() ); map: JApplet (getMainView returns JApplet): getContentPane().add( mainView.getMainView(), BorderLayout.CENTER ); menu: JApplet (getMenuView returns JApplet): getContentPane().add( menuView.getMenuView(), BorderLayout.WEST ); i want the menu to be a fixed width (180 pixels). For height i need it to be a minimum of 480, but it can be bigger. 1) is there a way to prevent the user from resizing it to anything below minimum size (automatically)? I have researched this but any document i found was older than 1_5_0 (... i think). If not, i'll probably use something similar to this:
// modified from "Liz" at http://www.codecomments.com/archive245-2004-5-199793.html
public void componentResized(ComponentEvent e){
	Component c = e.getComponent();
	MyClass.HandleWindowResize(c.getX(), c.getY(), c.getSize().width, c.getSize().height);
}

public void handleResize(...){
	if(width < minWidth) resize = true;
	//etc
	resize(x,y);
}
It doesn't really bother me that a user could make the window smaller than it should be, long as it doesn't update while he's dragging (i would get arrays of size 0 or maybe even negative i dunno, it could produce some unpredictable things) :) 2) i've been messing around for a bit now, was having trouble getting the menu to show if i added the main (the main's still not showing, but that's another matter). In the end the problem was solved with setPreferredSize(Dimension d):
//menuView.setMinimumSize( new Dimension( 180, 480 ) );
//menuView.getContentPane().setMinimumSize( new Dimension( 180, 480 ) );
//menuView.setPreferredSize( new Dimension( 180, 480 ) );
menuView.getContentPane().setPreferredSize( new Dimension( 180, 480 ) );
why do lines 1-2 do absolutely nothing (is there any use to them?), out of lines 3-4 (or 1-2), which is preferable? They *seem* to have the same effect (but 1/3 set it for JApplet, where 2/4 set it for... JApplet's default JPanel, i think).
LorenzoGatti
LorenzoGatti
The minimum, preferred, and maximum sizes are component properties that the layout manager of that component's parent can freely ignore.
The BorderLayout Javadoc is very explicit:
Quote:
The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.

Setting the minumum or maximum size has no effect.
I'm surprised that
menuView.getContentPane().setPreferredSize( new Dimension( 180, 480 ) );
works; JApplet must deduce its preferred size from that of its contentPane.
The direct form is simpler and works for any component type.
Putting two JApplets in something's content pane seems a bit redundant; what about JPanels?

Omae Wa Mou Shindeiru
kVandaele
kVandaele
i'll switch in JPanels instead (just based it off a really old program i had laying around, and hey, it worked).

Thanks for the advice and the explanation :)

<edit> swapped in the JPanels, that old project was made with school lessons so i dunno why they made us do it that way, it's even clearer this way, thanks again

i did search the API (course, it's a big API, if you don't know where to look...), i'll keep that in mind from now on (that fields may be ignored) </edit>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.