Re: JPopupMenu positioning
- From: Thomas Fritsch <i.dont.like.spam@xxxxxxxxxxx>
- Date: Wed, 23 Jan 2008 12:04:14 GMT
Sabine Dinis Blochberger schrieb:
JPopupMenu positioning[...]
I have a button, which, when clicked, should show a popup menu alignd
with its lower right corner.
The issue is that the popup menu shows in a wrong place with the first
click on the button, and in the correct one on every subsequent click,
even when moving or resizing the window.
source to display the problem:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {[...]
// show the popupmenu
jPopupMenu1.show(jButton1, jButton1.getWidth() -
jPopupMenu1.getWidth(), jButton1.getHeight());
}
I've reproduced your problem. In the debugger I saw:
When the method is called for the first time, jPopupMenu1.getWidth()
returns 0, because the popup (a package-private member variable in
JPopupMenu) is not yet realized.
When it is called the for second/third/... time, jPopupMenu1.getWidth()
returns 20, and all is fine.
[...]
Any way to make it behave the way I want to (show up in the correct
place on the first try)? I have tried
JPopupMenu.pack()I tried that, too, and didn't succeed.
Looking into JPopupMenu's source code, you see that JPopupMenu.pack() is
essentially a no-op, when JPopupMenu's popup member is not yet realized.
I would take a pragmatic route, and decide to display the popup menu
I also tried to set the popup menus on screen location directly, but
never found out how to get the correct point. This might be a possible
solution, to call jPopupMenu1.setLocation().
*left*-aligned, instead of *right*-aligned to the button:
private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
// show the popupmenu
jPopupMenu1.show(jButton1, 0, jButton1.getHeight());
}
I know this is not quite what wanted, but at least it behaves consistently.
--
Thomas
.
- Follow-Ups:
- Re: JPopupMenu positioning
- From: Sabine Dinis Blochberger
- Re: JPopupMenu positioning
- References:
- JPopupMenu positioning
- From: Sabine Dinis Blochberger
- JPopupMenu positioning
- Prev by Date: how do you disable checkbox from formAction class in struts framework?
- Next by Date: Re: JPopupMenu positioning
- Previous by thread: JPopupMenu positioning
- Next by thread: Re: JPopupMenu positioning
- Index(es):