msmolyak
asked on
Positionin a popup menu
I am creating a Swing-based application. One of the frames in that application displays a tree. When user clicks with a right mouse button on a tree node a popup menu apperas. My question is related to the positioning of that menu.
Does anyone has a code or an algorithm which allows to position a popup menu in such a way that the menu is always completely visible (that is it is not completetly or partially hidden by the edge of the screen or edge of the parent frame. That means it has to be positioned based on the cursor location as well as various edges. I am looking for a behavior similar to that of popup menus in Microsoft Windows Explorer in 95 and NT.
I am not looking for a general description of the approach I shoud take but rather for an exact algorithm or even better a code which implements such behavior.
Does anyone has a code or an algorithm which allows to position a popup menu in such a way that the menu is always completely visible (that is it is not completetly or partially hidden by the edge of the screen or edge of the parent frame. That means it has to be positioned based on the cursor location as well as various edges. I am looking for a behavior similar to that of popup menus in Microsoft Windows Explorer in 95 and NT.
I am not looking for a general description of the approach I shoud take but rather for an exact algorithm or even better a code which implements such behavior.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I slightly modified you logic to come up with
// Position the menu on the screen
Dimension sz=menu.size();
Dimension scrsz=Toolkit.getDefaultTo olkit().ge tScreenSiz e();
if (scrsz.width - (parentFrame.getLocationOn Screen().x + x) < sz.width)
{
// If the cursor is close to the right edge of the screen,
// position the menu to the left of the cursor
x = (parentFrame.getLocationOn Screen().x + x) - sz.width;
}
else
{
// Position the menu to the right of the cursor
x = parentFrame.getLocationOnS creen().x + x + 10;
}
y = Math.min(parentFrame.getLo cationOnSc reen().y + y + 25,
scrsz.height-sz.height);
this.menu.setLocation(x,y) ;
Thanks for your help.
// Position the menu on the screen
Dimension sz=menu.size();
Dimension scrsz=Toolkit.getDefaultTo
if (scrsz.width - (parentFrame.getLocationOn
{
// If the cursor is close to the right edge of the screen,
// position the menu to the left of the cursor
x = (parentFrame.getLocationOn
}
else
{
// Position the menu to the right of the cursor
x = parentFrame.getLocationOnS
}
y = Math.min(parentFrame.getLo
scrsz.height-sz.height);
this.menu.setLocation(x,y)
Thanks for your help.
sorry.