? dimensions of a JPanel before it's first painted?

T

TL

Hi,
How can I get the dimensions of a JPanel before it's first painted the
first time?

I.e, I have a Form (JDialog or JFrame) which uses a BorderLayout, and
has 2 JPanels on it. The first JPanel has its height computed at
runtime (eg 90 pixels), and the other takes up the remainder of the
Form. I call getBounds() on the second JPanel (the last thing in the
constructor) and it returns me width,height=0,0. However if I call
getBounds() in the Forms paint method, it gives me 1024,631 (which is
what I want to know in the constructor).

So basically it seems that the dimensions of the second JPanel are not
set until the Form is rendered and painted for the first time.
I want to use getBounds because it takes into account not only my
first JPanel, but also the Forms title bar, windows taskbar (if
visible), etc. (which are also dependent on the active look & feel),
so saves me lots of work.

How do I do this...?
TIA
 
A

Andrew Thompson

So basically it seems that the dimensions of the second JPanel ..

are not
set until the Form is rendered and painted for the first time.

setVisible(true) is the critical moment
that the entire process is called.
I want to use getBounds

For what, when, why?
..because it takes into account not only my
first JPanel, but also the Forms title bar, windows taskbar (if
visible), etc. (which are also dependent on the active look & feel),
so saves me lots of work.

So get the bounds after you JFrame
is set visible.

It sounds like you migh be doing some odd
things in your code, but the only way to tell
is to see a complet (*short*) exmaple..
<http://www.physci.org/codes/sscce.jsp>
 
T

TL

Andrew Thompson said:
setVisible(true) is the critical moment
that the entire process is called.
Thanks, will explore that area
For what, when, why?
A good question that I was actually asking myself. I've written (for a
new project) several base class 'Forms' (based on JDialog and JFrame
classes) that have several standard controls and a general-purpose
JPanel (on which subclasses can put controls). A couple of developers
are already extending them to build the forms used in the program. In
one of them the developer has used the size of the JPanel to influence
the size of some of the controls.
I asked myself the same question - is this necessary since the forms
are mostly using well-behaved scaleable grid- border- box- layouts,
etc. However I still want to ge able to provide the JPanel size
accurately (should we ever need it).
So get the bounds after you JFrame
is set visible.
But that's ugly - don't want the Form to flash up on the screen and
then instantly get redrawn because we're modifying the layout straight
away. That's the reason to getBounds during construction, so we don't
get that horrible flicker (even if it is only when first displayed).
Actually that reminds me of another issue (will post another msg)
It sounds like you migh be doing some odd
things in your code
if it's not odd it's no fun ;-)
 
A

Andrew Thompson

....
A good question that I was actually asking myself. I've written (for a
new project) several base class 'Forms' (based on JDialog and JFrame
classes) that have several standard controls and a general-purpose
JPanel (on which subclasses can put controls). A couple of developers
are already extending them to build the forms used in the program. In
one of them the developer has used the size of the JPanel to influence
the size of some of the controls.

I *think* this nmight be one of those "don't
ever do anything that stupid" type of things,
but I am too tired to know right now (and I
am hoping there are some GUI masters out there
might jump in any time..)

If you need to pursue it much furher I would
advise you take it out of the hands of these
...hacks and take it over to c.l.j.gui, where
the GUI folks actually hang out ( ducks quickly
and starts loping away.. ;-)
 
T

TL

Andrew Thompson said:
I *think* this nmight be one of those "don't
ever do anything that stupid" type of things,
but I am too tired to know right now (and I
am hoping there are some GUI masters out there
might jump in any time..)

If you need to pursue it much furher I would
advise you take it out of the hands of these
..hacks and take it over to c.l.j.gui, where
the GUI folks actually hang out ( ducks quickly
and starts loping away.. ;-)

I remember now a specific case - in one help window we want to have a
table (size 3x2) and various parts of text within this table is
highlighted. Eg the start of a word is highlighted, the end not.
This was very easy to do in html (putting it in a JLabel), which also
included the table definition as html. Cool! However if the JPanel was
300 pixels wide and the table was set to 100%, it wouldn't stretch to
the full width if there wasn't much text in it.
The only way we could make the html table use the full width of the
panel was to explicitly state the size of it (in pixels).
Hence our desire to know the true dimensions of the JPanel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top