Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2004

Tip: Looking for answers? Try searching our database.

window size problem

Thread view: 
Gergely Buday - 05 Apr 2004 13:18 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.