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 / November 2003

Tip: Looking for answers? Try searching our database.

Intelligent PACK requestied.

Thread view: 
Roedy Green - 31 Oct 2003 22:00 GMT
I have three problems with JFrame.setSize

1. The size of the frame depends on just which components are
currently visible.  It is a bit of a hassle trying to compute what it
should be.

2. If I use pack, it does goofy things like take the entire screen or
create a long skinny horizonal window that goes all the way across the
screen.

3. When I select custom sizes for the various configurations, they
look fine on my machine, but when I run them on the client's, he
complains some fields are not visible in the default frame size.

What I would like is an intelligent pack that could be given some
hints, e.g. the desired aspect ratio, max width or max height, that
possibly would avoid windows bigger than the screen size etc. one that
would select magically the frame size some one using the Frame would
manually set for himself.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green - 01 Nov 2003 01:29 GMT
>I have three problems with JFrame.setSize

There is one more problem with setting frame sizes. GridBagLayout is
not very predictable.  Sometimes it gives extra space to the left
column and sometimes to the right, ignoring my suggestions.  This
makes it hard to guess the size of a frame to enclose the entire mess
My layout has many optional portions so it pretty well have to
calculate a size rather than just find nice sizes empirically.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark 'Kamikaze' Hughes - 03 Nov 2003 00:43 GMT
Roedy Green <roedy@seewebsite.com>
wrote on Sat, 01 Nov 2003 00:29:24 GMT:
>>I have three problems with JFrame.setSize
> There is one more problem with setting frame sizes. GridBagLayout is
[quoted text clipped - 3 lines]
> My layout has many optional portions so it pretty well have to
> calculate a size rather than just find nice sizes empirically.

 Huh?  I've never seen that behavior before.  GBL's a pain to use, but
it's always been very reliable.  Can you post some sample code?

 I use a utility method to reset the GridBagConstraints to my preferred
settings; doing this or just newing a GBC every time will solve a lot of
subtle problems.

Signature

<a href="http://kuoi.asui.uidaho.edu/~kamikaze/"> Mark Hughes </a>
"I believe in communication.  If I communicate with you every so often,
you'll be bothered by what I say enough that you won't ask me to, which
means more sleep for me." -Something Positive, 2003Sep22

Roedy Green - 03 Nov 2003 01:25 GMT
>  Huh?  I've never seen that behavior before.  GBL's a pain to use, but
>it's always been very reliable.  Can you post some sample code?

To see the sort of layout I am talking about layout click
http://mindprod.com/replicator/replicatorwebsite.html

There are variants on it with more fields. The catch is the proportion
of space to the left and right of the centreline changes when you add
more fields.

There are two sorts of answer to this question:

1. stand on one leg on a full moon, put your finger up your nose, cut
off your left arm, then it will work.

2. This is RIDICULOUS.  I HAVE HAD ENOUGH AND I WON'T TAKE IT ANY
MORE.  I want a layout that is docile and does what I tell it EASILY.
What we have acts like a Louisiana lawyer trying to wriggle out of
doing what I want.  There has to be a better way!

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark 'Kamikaze' Hughes - 03 Nov 2003 23:11 GMT
Roedy Green <roedy@seewebsite.com>
wrote on Mon, 03 Nov 2003 01:25:49 GMT:
>>  Huh?  I've never seen that behavior before.  GBL's a pain to use, but
>>it's always been very reliable.  Can you post some sample code?
> To see the sort of layout I am talking about layout click
> http://mindprod.com/replicator/replicatorwebsite.html

 404, and the directory contains no source code.  I don't want to see
your whole app, anyway, just the GUI parts.

> There are variants on it with more fields. The catch is the proportion
> of space to the left and right of the centreline changes when you add
> more fields.

 Without seeing the code, I can't tell you exactly what you're doing
wrong, but it's almost certainly a result of incorrect values in your
GridBagConstraints.  If you take a look at the Javadocs for GBC and GBL,
it's pretty clear.

 For a start, only use gbc.anchor=gbc.NORTHWEST (the default CENTER is
usually more attractive, but it's probably misleading you as to where a
cell starts), set gridx, gridy to your cell (I don't recommend using the
default RELATIVE until you know what that does), and use the default
values (listed in the Javadoc) for other fields, until you get the
basics down right.  Maybe use gridwidth, gridheight if you have to do a
colspan/rowspan kind of thing.

> There are two sorts of answer to this question:
> 1. stand on one leg on a full moon, put your finger up your nose, cut
[quoted text clipped - 3 lines]
> What we have acts like a Louisiana lawyer trying to wriggle out of
> doing what I want.  There has to be a better way!

 Both of those answers are completely wrong, falsely blaming the tools
rather than the workman.  The third sort of answer, the correct one, is
"RTFM".

 It's a moderately complicated tool, but that's because layout is a
complicated problem.  It does *everything*, and its behavior is
rigorously correct and consistent, in my long experience with it; every
time I've had a problem with it, I've always kicked myself for being an
idiot when I realize what I did wrong.  If all else fails, you have the
JDK source like everyone else.  Read it.

 The fourth meta-answer is, it's not hard to write layout managers.  If
you're dissatisfied with GBL, write your own and use that.  A layout
that takes a Rectangle of grid coords as the constraints, and does not
resize its components, would be easy enough to write.

Signature

<a href="http://kuoi.asui.uidaho.edu/~kamikaze/"> Mark Hughes </a>
"I believe in communication.  If I communicate with you every so often,
you'll be bothered by what I say enough that you won't ask me to, which
means more sleep for me." -Something Positive, 2003Sep22

Tor Iver Wilhelmsen - 01 Nov 2003 01:37 GMT
> 1. The size of the frame depends on just which components are
> currently visible.  It is a bit of a hassle trying to compute what it
> should be.

That's what pack() is for.

> 2. If I use pack, it does goofy things like take the entire screen
> or create a long skinny horizonal window that goes all the way
> across the screen.

That's what proper use of layout managers and preferredSize is for.

> What I would like is an intelligent pack

No, pack() should ask an intelligent layout manager.
Roedy Green - 01 Nov 2003 01:30 GMT
>That's what pack() is for.

But my point is pack is stupid.  It generates long skinny frames or
needlessly takes up the whole screen.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Mark 'Kamikaze' Hughes - 03 Nov 2003 00:39 GMT
Roedy Green <roedy@seewebsite.com>
wrote on Sat, 01 Nov 2003 00:30:00 GMT:
>>That's what pack() is for.
> But my point is pack is stupid.  It generates long skinny frames or
> needlessly takes up the whole screen.

 Only if you have a layout that says to do that.  Sure, if you use
FlowLayout, pack() will dutifully fill your entire screen across before
wrapping, because that's what you asked for.

 If you want a different layout, say to wrap when you get to 50% of the
desktop width, you'll have to code that yourself.  Use a GridBagLayout
or my StackLayout2 (on my site) to make a vertical layout, and inside
that put FlowLayouts, and call getPreferredSize() after each component
is added to the current FlowLayout.  When you get over 50%, start
another row.

Signature

<a href="http://kuoi.asui.uidaho.edu/~kamikaze/"> Mark Hughes </a>
"I believe in communication.  If I communicate with you every so often,
you'll be bothered by what I say enough that you won't ask me to, which
means more sleep for me." -Something Positive, 2003Sep22

Roedy Green - 01 Nov 2003 01:31 GMT
>That's what proper use of layout managers and preferredSize is for.

The elements HAVE a natural size based on current contents. You
should not have to fart around guessing other sizes.  
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Babu Kalakrishnan - 01 Nov 2003 07:06 GMT
>>That's what proper use of layout managers and preferredSize is for.
>
>  The elements HAVE a natural size based on current contents. You
> should not have to fart around guessing other sizes.  

I agree. Calling setPreferredSize on a component is generally a bad
practice because it overrides the natural size calculated by its layout
manager. You should generally use it only on containers whose have some
flexibility in accepting variable preferredSizes (like for instance a
ScrollPane)

As for your problem, I think there is no quickfix solution. pack() isn't
really that dumb - It asks the contentPane's Layout Manager for its
preferred size (which is based on what its contents, and their
sub-contents are), and allocates a size after adding the size for
decorations menu etc.

So it is entirely upto the GUI designer to use appropriate
LayoutManagers for the contentPane as well as Panels below it and
position components appropriately (and specify their constraints) so as
to display the contents with an aesthetically pleasant layout. This
requires the GUI designer to be able to guess the expected sizes of each
component in the GUI - not to a pixel level accuracy, but a ballpark
figure at least.

And yes - some LayoutManagers are pretty quirky in their behaviour. The
behaviour is not really unpredictable, but rather different from what
one would intuitively expect. (For instance with a GridBagLayout when
the available space is even a single pixel less than the required
preferredSize in either the X or Y direction, you find all components
shrinking to their minimum sizes in both the X and Y directions). To be
able to use such LayoutManagers correctly, one needs a clear
understanding of their expected behaviour (/quirks) - and unfortunately
that is often not that easy - especially for new users. So I generally
recommend new users to use LayoutManagers that are more predictable -
even if it means having to use several nested panels.

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.