Hi,
I'm a new user of javax.swing, so my problem might be trivial.
The problem is that upon starting my program, it gives a very small window,
giving only a cca. 15 character wide window header. Of course I can resize
my window, but that's not what I want.
I do use JFrame.pack:
============================================================================
=
JFrame frame = new JFrame("header");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
MainPanel mainPanel = new MainPanel();
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
============================================================================
==
public class MainPanel extends JPanel {
public MainPanel() {
SpringLayout layout = new SpringLayout();
setLayout(layout);
[...]
HeaderPanel headerPanel = new HeaderPanel(ppanel);
add(headerPanel);
add(rlScrollPanel);
layout.putConstraint(SpringLayout.WEST, headerPanel,
5,
SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, headerPanel,
5,
SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.NORTH, rlScrollPanel,
10,
SpringLayout.SOUTH, headerPanel);
layout.putConstraint(SpringLayout.WEST, rlScrollPanel,
15,
SpringLayout.WEST, this);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
}
=======================================================
Do you have any idea how to solve my problem?
Thanks in advance
- Gergely
Gregory A. Swarthout - 05 Apr 2004 21:31 GMT
> Hi,
>
[quoted text clipped - 53 lines]
>
> - Gergely
You either have to set your component sizes and tell the JFrame to be
just big enough to display them all (pack()) or tell your JFrame how
big to be and let the layout manager decide how big to make each
component (JFrame.setSize()). Right now you are doing neither.
Pete Wright - 05 Apr 2004 23:41 GMT
> You either have to set your component sizes and tell the JFrame to be
> just big enough to display them all (pack()) or tell your JFrame how
> big to be and let the layout manager decide how big to make each
> component (JFrame.setSize()). Right now you are doing neither.
Those suggestions don't actually apply when using the SpringLayout added
in 1.4 of Java. With the SpringLayout you effectively set up constraints
to control spacing and positioning of components and then add constraints
to the parent frame to control it's spacing on the right and bottom edges
to allow a boundary around the spring controlled contained components.
Pete Wright - 05 Apr 2004 23:32 GMT
Hi yah,
My initial thoughts on this are that you haven't added the springs to the
container itself. You've set the headerPanel to appear 5 pixels in from
the left of the container, and 5 pixels down. You've also set the
scrollPanel to appear 10 pixels down from the bottom of the headerPanel
and 15 pixels in from the left of the container. That's all well and good
and the panels will appear the right size. The container frame though
won't since you haven't set springs for the container's EAST and SOUTH
edges, like so
layout.putConstraint( SpringLayout.EAST, this, 5,
SpringLayout.EAST, headerPanel );
layout.putConstraint( SpringLayout.SOUTH, this, 5,
SpringLayout.SOUTH, rlScrollPanel );
If you put those two constraints in, you should find the container frame
looks a lot better.
Hope that helps,
Pete Wright
Author of ADO.NET Novice To Pro,
From Apress Inc.
> Hi,
>
[quoted text clipped - 53 lines]
>
> - Gergely
Gergely Buday - 06 Apr 2004 10:26 GMT
> My initial thoughts on this are that you haven't added the springs to the
> container itself.
[quoted text clipped - 5 lines]
> layout.putConstraint( SpringLayout.SOUTH, this, 5,
> SpringLayout.SOUTH, rlScrollPanel );
You are right.
My question was really RTFM, the Swing Tutorial had the answer. Just when
you are stuck, you are stuck indeed.
Thanks for all of the responses.
- Gergely
Thomas J. Clancy - 06 Apr 2004 03:36 GMT
Try setting the preferred size of the components within your frame. pack()
will pack the outer frame around the preferred size of its outermost child.
At least it seems to work for me this way.
tom
> Hi,
>
[quoted text clipped - 5 lines]
>
> I do use JFrame.pack:
============================================================================
> =
> JFrame frame = new JFrame("header");
[quoted text clipped - 5 lines]
> frame.pack();
> frame.setVisible(true);
============================================================================
> ==
> public class MainPanel extends JPanel {
[quoted text clipped - 32 lines]
>
> - Gergely
Babu Kalakrishnan - 06 Apr 2004 07:11 GMT
> Try setting the preferred size of the components within your frame. pack()
> will pack the outer frame around the preferred size of its outermost child.
> At least it seems to work for me this way.
In general this is bad advice. Setting explicit values for preferredSize should
be avoided if possible, and one should allow each container to determine its own
preferredSize based on the preferredSize of children that it contains, and the
LayoutManager used.
As far as the OP's problem is concerned, I think the problem is with not setting
the constraints of the SpringLayout properly (as pointed out by another poster).
Having never used SpringLayout myself, I'm not knowledgeable enough to pinpoint
the exact error, but I guess reading the chapter on SpringLayout in Sun's Java
Tutorial would probably help - since there is an example shown in it that
exhibits exactly the same behaviour.
BK