I've been having sizing problems with java GUIs and I think I must be
missing something, because nothing seems to ever size how I'd like.
Right now i am trying to create a gui with 6 components. There are 2
JTrees, 2 JPanels, and 2JTextPanes. I have the interface layed out in grids
of 75 pixels, so everything in my layout is sized in multiples of 75. It's
supposed to line up so that the leftmost colum is 150 px wide and contains
a square JPanel above a JTree. There is a JTextPane that runs along the top
to the right of the left most column. It's 150 high , so it lines up with
the JPanel in the upper left corner. Under the JPanel is a split pane with
a content JPanel on the left (375x375), and to the right of the content
JPanel is a column with a 150x150 JTree above a 150 x whatever's left
JTextPane. Unfortunately, nothing is sizing how i want. Am I missing
something obvious? Right now I'm setting everything with a preferredsize so
that I can just get the porper ratios, but that will change so that i can
readjust the size of components.
Here is my code
JPanel actions = new JPanel();
actions.setPreferredSize(new Dimension(150,150));
JTree projectTree = new JTree();
ScrollPane projectTreeScroll = new ScrollPane();
projectTreeScroll.add(projectTree);
projectTree.setPreferredSize(new Dimension(150,375));
JSplitPane leftSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
actions, projectTreeScroll);
JTextPane textOutput = new JTextPane();
ScrollPane textScroll = new ScrollPane();
textScroll.add(textOutput);
textOutput.setPreferredSize(new Dimension(430, 150));
JPanel content = new JPanel();
content.setPreferredSize(new Dimension(375,375));
JTree helpTree = new JTree();
ScrollPane helpTreeScroll = new ScrollPane();
helpTreeScroll.add(helpTree);
helpTree.setPreferredSize(new Dimension(150,150));
JTextPane helpDoc = new JTextPane();
ScrollPane helpScroll = new ScrollPane();
helpScroll.add(helpDoc);
helpDoc.setPreferredSize(new Dimension(150,300));
JSplitPane helpSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
helpTreeScroll, helpScroll);
helpSplit.setPreferredSize(new Dimension(150, 375));
JSplitPane contentHelpSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
content, helpSplit);
JSplitPane rightSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
textScroll, contentHelpSplit);
JSplitPane main = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftSplit, rightSplit);
this.getContentPane().add(main);
this.pack();
this.setVisible(true);
Any help would be wonderful.
John McGrath - 17 Mar 2005 05:38 GMT
> Unfortunately, nothing is sizing how i want. Am I missing
> something obvious?
One thing that is missing is telling us in what way the components are not
sized the way that you want. Are they off by a little? Completely the
wrong size? All in the wrong place?
One way to let people see what is happening would be to post a runnable
example of the problem. That way, people could run the code and see what
is happening.
I suspect that part of your problem may be that you are not accounting for
the added size of the JSplitPanes and JScrollPanes.
One thing that may help is to run your program, make sure the window in
question has focus, and then press Ctrl-Shift-F1. That will do a list()
on the window, which can sometimes clear up what is going on with the
layout. Try that and look at the sizes and positions for the components.

Signature
Regards,
John McGrath
Thomas Weidenfeller - 17 Mar 2005 09:58 GMT
> Right now i am trying to create a gui with 6 components. There are 2
> JTrees, 2 JPanels, and 2JTextPanes. I have the interface layed out in grids
> of 75 pixels, so everything in my layout is sized in multiples of 75.
Use Java's own layout managers and don't hard-code positions.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
John McGrath - 17 Mar 2005 13:35 GMT
> > Right now i am trying to create a gui with 6 components. There are 2
> > JTrees, 2 JPanels, and 2JTextPanes. I have the interface layed out in
> > grids of 75 pixels, so everything in my layout is sized in multiples
> > of 75.
>
> Use Java's own layout managers and don't hard-code positions.
There are no hard-coded positions in the code, only hard-coded preferred
sizes.

Signature
Regards,
John McGrath