coordinates for Frame client area

M

Mr B.

Hi

How do I obtain the coordinates for the clientarea of a window, ie adjusted
for caption, menubar, framewidth etc etc

TIA
 
T

Thomas Weidenfeller

Mr said:
How do I obtain the coordinates for the clientarea of a window, ie adjusted
for caption, menubar, framewidth etc etc

In general, you don't. You leave positioning and sizing of components to
layout managers. Trying to circumvent layout managers does usually
more harm than good.

But if you really want to mess around with your own layout algorithm,
get the size and get the insets of the frame. Then do the math. But
please don't come back later and complain that your application does
look bad on other platforms, VMs, operating systems, etc. (Q3.3 of the FAQ).

/Thomas
 
M

Mr B.

...
In general, you don't. You leave positioning and sizing of components to
layout managers. Trying to circumvent layout managers does usually more
harm than good.

Really ?
Im happy that is not what Im trying to do then....
But if you really want to mess around with your own layout algorithm, get
the size and get the insets of the frame. Then do the math. But please
don't come back later and complain that your application does look bad on
other platforms, VMs, operating systems, etc. (Q3.3 of the FAQ).

So getInsets was the answer.... tsk tsk....
 
L

Larry Barowski

Mr B. said:
Hmm... the JPanels are redrawn when the JFrame redraws, I guess that could
be the problem.
I am using NetBeans 3.6, and the JFrames are placed at designtime, not
created at runtime, so I am not sure how to override their paint method..

They are created at runtime. Even though you are
using a gui builder, it just produces code that does a
"new JPanel()" somewhere.

You need to use JPanel subclasses that either repaint
themselves or notify the code responsible for painting
them when they get a repaint request. Something like
a "repaint listener" would be a flexible way to
implement that.

A sloppy fix might be to go up the component
hierarchy from the JPanels to the root pane and
setOpaque(false) for each component (they will all
be JComponents). I think that should work but it
will cause some unnecessary repainting.
 
L

Larry Barowski

Mr B. said:
...


Really ?
Im happy that is not what Im trying to do then....

It was pretty good guess though. It's also highly likely
that anything you're trying to do that requires frame insets
is a bad idea, or that there is a better way to do it. So what
are you trying to do?
 
L

Larry Barowski

Mr B. said:
Yes, that was pretty much the way I was heading.

It presents me with several problems..... will I lose the GUI if I subclass
ex a JPanel ?

I'm sure you can tell the gui builder in NetBeans to use your
JPanel subclass. I don't use NetBeans, so I can't help you
out on that. You could also, of course, just add an instance
of your subclass to the JPanels (with a BorderLayout set on
the panels).
 
B

Birkemose

...
It was pretty good guess though. It's also highly likely
that anything you're trying to do that requires frame insets
is a bad idea, or that there is a better way to do it. So what
are you trying to do?

I don't mind his advice, I mind his "tone", and after reading several other
of his replies, I guess I got slightly annoyed ;)

I was trying to adjust the position of a pop-up menu.
The idea is the pop-up should be used for all overlaying componenets (
drawing areas ), ie I pop up relatively to the basic JFrame.
I can adjust the pop-up position for whetever component I like, but I was
missing the menu and borders of the JFrame.

Why would getInsest not work on other platforms ?

TIA
 
R

Roland

It was pretty good guess though.
It was still a guess. And as OP indicated that he's not trying to
circumvent layout managers, I would call it an uneducated guess.
It's also highly likely
that anything you're trying to do that requires frame insets
is a bad idea, or that there is a better way to do it.
And yet there also a possibilty that it's not. Maybe OP is developing
components for a custom look and feel.
So what
are you trying to do?

OP's original question was posed in a neutral fashion, IMO. Instead of
making assumptions, I think your question "So what are you trying to
do?" would have gotten more information from OP and probably would have
avoided OP's reaction to Thomas' reply.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
L

Larry Barowski

Birkemose said:
I was trying to adjust the position of a pop-up menu.
The idea is the pop-up should be used for all overlaying componenets (
drawing areas ), ie I pop up relatively to the basic JFrame.
I can adjust the pop-up position for whetever component I like, but I was
missing the menu and borders of the JFrame.

Why would getInsest not work on other platforms ?

It doesn't have any consistent meaning. If you do
JFrame.setDefaultLookAndFeelDecorated(true), for
Look & Feels and systems that support it, the frame
insets will be (0, 0, 0, 0) even though there is a border
and title bar. DefaultLookAndFeelDecorated is just a
hint, so any particular Look & Feel might choose to
provide its own title bar and borders despite the
setting, with the same result.

I'm not sure exactly what you're after though.

If you want the area not including the title bar, menus,
and borders, then use the bounds of the content pane.

If you want the area of the JFrame excluding the title
bar and borders but including the menu, I'm not sure
there is any guaranteed way to get that. Using the root
pane bounds and insets will get you closer, but still
includes the title bar if it is provided by the Look &
Feel. I suppose you could use the union of the bounds
of the menu bar and content pane, though I'm not
sure offhand what would happen on Mac Look & Feel
with the menu at the top of the screen.
 
M

Mr B.

...
It doesn't have any consistent meaning. If you do
JFrame.setDefaultLookAndFeelDecorated(true), for
Look & Feels and systems that support it, the frame
insets will be (0, 0, 0, 0) even though there is a border
and title bar. DefaultLookAndFeelDecorated is just a
hint, so any particular Look & Feel might choose to
provide its own title bar and borders despite the
setting, with the same result.

I'm not sure exactly what you're after though.

If you want the area not including the title bar, menus,
and borders, then use the bounds of the content pane.

If you want the area of the JFrame excluding the title
bar and borders but including the menu, I'm not sure
there is any guaranteed way to get that. Using the root
pane bounds and insets will get you closer, but still
includes the title bar if it is provided by the Look &
Feel. I suppose you could use the union of the bounds
of the menu bar and content pane, though I'm not
sure offhand what would happen on Mac Look & Feel
with the menu at the top of the screen.

Thanks for your insight on this...

Basicly I have a JFrame containing several JPanels with some linedrawing on
top
I found, that adding pop-up's to JPanels with custom drawing on top is
difficult because of redraw problems, simply because the OS is not aware of
my custom drawing and does some "random" background erasing on the JPanels
when the pop-up is displayed...
Keeping the JFrame the parent of the pop-up fixes all that in an easy way,
because now the OS knows it has to redraw the JPanels.

Drawback is that my pop-up coordinates has to be translated from JPanel to
JFrame...
I first added JPanel X,Y but found I missed the "borders and captions"....
hence my original question

But your comment made me think....
Maybe converting from JPanel to global coordinates, and then back into
JFrame coordinates would be safer ?
 
L

Larry Barowski

Mr B. said:
Basicly I have a JFrame containing several JPanels with some linedrawing on
top
I found, that adding pop-up's to JPanels with custom drawing on top is
difficult because of redraw problems, simply because the OS is not aware of
my custom drawing and does some "random" background erasing on the JPanels
when the pop-up is displayed...

You should get a repaint event for any erased area.
Are you doing the painting from outside the JPanels,
and not repainting when the JPanels receive repaint
requests? If so I would rethink your design. Your
solution may not work on all systems, and you are
likely to have other painting problems.
Keeping the JFrame the parent of the pop-up fixes all that in an easy way,
because now the OS knows it has to redraw the JPanels.

Drawback is that my pop-up coordinates has to be translated from JPanel to
JFrame...
I first added JPanel X,Y but found I missed the "borders and captions"....
hence my original question

But your comment made me think....
Maybe converting from JPanel to global coordinates, and then back into
JFrame coordinates would be safer ?

Sure, that is simple and guaranteed to work. You could
also go up the component hierarchy from the panel to
the frame and compute the total offset, but that would
be more work.
 
M

Mr B.

...
You should get a repaint event for any erased area.
Are you doing the painting from outside the JPanels,
and not repainting when the JPanels receive repaint
requests? If so I would rethink your design. Your
solution may not work on all systems, and you are
likely to have other painting problems.

Hmm... the JPanels are redrawn when the JFrame redraws, I guess that could
be the problem.
I am using NetBeans 3.6, and the JFrames are placed at designtime, not
created at runtime, so I am not sure how to override their paint method..
 
M

Mr B.

...
They are created at runtime. Even though you are
using a gui builder, it just produces code that does a
"new JPanel()" somewhere.

I think I knew that ;)
You need to use JPanel subclasses that either repaint
themselves or notify the code responsible for painting
them when they get a repaint request. Something like
a "repaint listener" would be a flexible way to
implement that.

Yes, that was pretty much the way I was heading.

It presents me with several problems..... will I lose the GUI if I subclass
ex a JPanel ?
If not, any changes to NetBeans must be through the project files, otherwise
the company that hired me for this, will not be able to recompile... ( and
the project should be able to recompile on a pretty much fresh install of
NetBeans with java 1.4.x )
Losing the GUI part of the design by subclassing the JPanel would not be an
option, the project would be impossible to maintain....

Anyway, thanks a lot for your thoughts on this
 
B

Birkemose

...
I'm sure you can tell the gui builder in NetBeans to use your
JPanel subclass. I don't use NetBeans, so I can't help you
out on that. You could also, of course, just add an instance
of your subclass to the JPanels (with a BorderLayout set on
the panels).

I also though of this...
Drawback is, that any programmer that will have to build on my work in the
future, will have to know this.... the getInsets hack is at least only in my
code ;)
Also, even if I understand the pitfalls in getInsets, the worst thing that
could happen is a slightly misplaced pop-up, hardly a showstopper, so until
I get more java under my belt, I think I will stick to that solution.

Again, thanks for your time and insight on this :)
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top