Hot to get dimensions of a JPanel before it's first painted?  
Author Message
Phil H黨n





PostPosted: 2004-8-3 21:16:00 Top

java-programmer, Hot to get dimensions of a JPanel before it's first painted? 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) 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 the
reduced size due to 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.

One suggestion is to call setVisible(true) in the constructor but this
can be ugly either with a flicker or the dialog stays on the screen
(hmmm, haven't tried making it non-modal :-[ ). Calling setVisible(true)
did work, however, for JFrames.

You may ask why we need to know the JPanel size anyway if we are using
well-behaved scaleable grid- border- box- layouts, etc. Here's one example:
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.

Is it possible to get the JPanel dimensions before it's first painted?
TIA

BTW, using jdk 1.4.2, Win XP SP1.
 
Phil H黨n





PostPosted: 2004-8-3 21:49:00 Top

java-programmer >> Hot to get dimensions of a JPanel before it's first painted? oops, that should have been "How..."
 
Laird Nelson





PostPosted: 2004-8-3 21:55:00 Top

java-programmer >> Hot to get dimensions of a JPanel before it's first painted? Phil H黨n wrote:
> Hi,
> How can I get the dimensions of a JPanel before it's first painted the
> first time?

Override addNotify(), which is called when a component is "realized".
pack() will do it too. For more on Swing components and what
realization means, see
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html#single_thread_rule.

Cheers,
Laird
 
 
Phil H黨n





PostPosted: 2004-8-4 22:02:00 Top

java-programmer >> Hot to get dimensions of a JPanel before it's first painted? Laird Nelson wrote:

> Phil H黨n wrote:
>> Hi,
>> How can I get the dimensions of a JPanel before it's first painted the
>> first time?
> Override addNotify(), which is called when a component is "realized".
> pack() will do it too. For more on Swing components and what
> realization means, see
> http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html#single_thread_rule.

Thanks for the tip, will try it out...
 
 
usenet





PostPosted: 2004-8-6 1:28:00 Top

java-programmer >> Hot to get dimensions of a JPanel before it's first painted? Phil H黨n <email***@***.com> wrote:

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

The question does not make sense, even if you replace "painted" by "layed
out". Only when it is first layed out does the JPanel get its proper
dimensions (before it will have some random leftover value, or initially
(0,0)). Before that, it is not known which dimension it will get (since
only then does the LayoutManager of the JPanel's parent container decide
how large it should get).

The position/size has nothing do with painting: if the JPanel happens to
be show up covered by another window, it may even never be painted at all.



Christian