views:

55

answers:

4

I'm using Netbean's form creator and I'm trying out some things. I'm not sure if it's the layout manager, but when I create my own JPanel and add it to the main content pane of the my window, the size of the panel always maximizes inside the FrameView no matter what re-dimensioning methodology I use such as setSize or setPreferred size. I'm very new to AWT and Swing.

+1  A: 

With NetBeans WYSIWYG designer it's a peace of cake - just be sure to use Free Form Design and use mouse for resizing. Maybe the panel is itself larger than FrameView, so double click on it (you are editing it exclusively) and make it smaller. Than double click to get back to parent component and you should be fine.

Or maybe check some tutorials at NetBeans site.

Xorty
A: 

In my experience, Swing is a very picky thing. I'd try setMaximumSize and setPreferedSize. As a side note though: when ever I used GridLayout, it always stretches whatever is in each of the cells (to make it symmetrical I guess). Flow, Box, Border, and I think GridBag don't have that problem though.

-Brett

Brett
setPreferredSize is the hint you want to give the layout manager.
Gilbert Le Blanc
+2  A: 

You're not supposed to set sizes manually. Leave that to the layout managers, and your GUI will not break when the window size or the font or even just a button label changes.

The trick with layout mangers is that you can use not just one but several, in nested JPanels. That way, nearly any layout is possible.

Michael Borgwardt
Better than nesting ad infinitum (or is that ad nauseum) is to use a table-oriented layout manager (http://www.google.com/search?q=table-oriented+layout+manager) for your primary layout manager.
Software Monkey
@Software Monkey: I would definitely not call that a strictly better approach; it depends on the requirements of the GUI. For form-like data entry UIs, a table-oriented layout is good. But not all UIs are like that and even then it would be a bad idea to try and make everything work with that single layout manager.
Michael Borgwardt
Alright thanks Michael. You're saying I shouldn't mess with the root pane, and that I should add controls to a layout-manager controlled pane, where these controls should have relative position like I'm expecting.I just dragged an edit control into a sub-pane as a test and it expanded into it's parent pane. I don't know property controls this behavior but more info on this would be appreciated.
chaz
@chaz: what I meant is that Java layout is controlled through the combination of layout managers, not through explicit size properties. This requires understanding of how the various layout managers work. For example, BorderLayout will give its NORTH, SOUTH, WEST and EAST children their preferred sizes (i.e. a JLabel child will get as much space as it needs for its text) and all remaining space to the CENTER child. So for example if you put all your actual content into NORTH (in an appriately subdivided JPanel) then resizing the frame will not cause it to stretch all over the place.
Michael Borgwardt
@Michael: I said *primary*, not *only* layout manager. My experience has been that on a typical UI, each panel with a table-oriented layout manager replaces numerous nested panels with the Java standard layouts. I expect some of the newer layout managers alleviate some of that, but not entirely.
Software Monkey
A: 

This happens because the layout manager of JPanel's container (perhaps it is either JFrame or another JPanel) instructs the JPanel to maximize.

Do the following:

  1. Find out, who is the parent of this JPanel (take a look at NetBeans's containment object tree)

  2. Check, what layout is defined for the container

  3. Read the documentation about LayoutManager's (they're in java.awt package)

  4. ???

  5. PROFIT!

Vanya