greg_100
asked on
popup menu goes out over screen size
I have a popup menu in my java application that parts of the menu goes out of view on the bottom and right side of the screen. is there any way to have the menu pop up insead of down and left instead of right in these cases?
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JPopupMenu.html#setLocation(int, int)
> is there any way to have the menu pop up insead of down and left instead of right in these cases?
you control the location it pops up, so yes :)
when you pop it up, check the screen dimensions, and the popup menu size and use that to decide the location to popup.
eg. to make it popup to the left, shift the mouse click position left by the width of the popup to get the popup location.
you control the location it pops up, so yes :)
when you pop it up, check the screen dimensions, and the popup menu size and use that to decide the location to popup.
eg. to make it popup to the left, shift the mouse click position left by the width of the popup to get the popup location.
The cleanest way to do this is to sub-class Java's implementation of JPopupMenu with your own custom class that overrides the show method. This will prevent the rest of your code having to worry about any of this. Here's a small sample to get you started, I won't provide the full listing, as there wouldn't be much fun in it for you!
public void show(Component invoker, int x, int y) {
if (invoker != null) {
Point pt = invoker.getLocationOnScree n();
Dimension preferredSize = getPreferredSize();
Dimension screenSize = Toolkit.getDefaultToolkit( ).getScree nSize();
// other code to work out new positions for x and y if go outside screen...
}
super.show(invoker, x, y);
}
There are a couple of problems with this though, mainly that it doesn't cater for the Windows toolbar, although there may be a way to cater for this in releases later than 1.3.1, but I seem to be the only one still stuck at that version :(. But it will still be better than not having anything at all.
public void show(Component invoker, int x, int y) {
if (invoker != null) {
Point pt = invoker.getLocationOnScree
Dimension preferredSize = getPreferredSize();
Dimension screenSize = Toolkit.getDefaultToolkit(
// other code to work out new positions for x and y if go outside screen...
}
super.show(invoker, x, y);
}
There are a couple of problems with this though, mainly that it doesn't cater for the Windows toolbar, although there may be a way to cater for this in releases later than 1.3.1, but I seem to be the only one still stuck at that version :(. But it will still be better than not having anything at all.
Or if you are having dificulties with proper drop-down menues, then create a custom implementation of JMenu...
ASKER
My calculations so far are:
Get screen size, Get Mouse Location, get Menu size. convert screen size to pixels.
I check to see if mouse location + menu width is > screen size and translate the popup.
I also check to see if the mouse location + menu height is > screen size and translate the popup.
But this is not working correctly. am I missing something or calculating something wrong?
Get screen size, Get Mouse Location, get Menu size. convert screen size to pixels.
I check to see if mouse location + menu width is > screen size and translate the popup.
I also check to see if the mouse location + menu height is > screen size and translate the popup.
But this is not working correctly. am I missing something or calculating something wrong?
possibly an error in your calcs, how exactly is it not working?
ASKER
When I click to bring up the popup menu near the right side or bottom of the screen I only see part of the menu as the other part is off the screen.
Could you provide the code that you are using, then we may be able to help you some more...
ASKER
// get menu size
Dimension menuSize = _popupMenu.getPreferredSiz e();
//get the location of the Mouse
Point absolutePosition = new Point();
SwingUtilities.convertPoin tToScreen( absolutePo sition, evt.getComponent());
//get screen size
Dimension screenSize = Toolkit.getDefaultToolkit( ).getScree nSize();
// get menu location
Point menuLocation = evt.getPoint();
// check width
if(absolutePosition.x + menuSize.width > screenSize.width){
// adjust width
menuLocation.translate(-me nuSize.wid th,0);
} //if
// check height
if(absolutePosition.y + menuSize.height > screenSize.height){
// adjust height
menuLocation.translate(0,- menuSize.h eight);
} //if
_popupMenu.show(this, menuLocation.x, menuLocation.y);
return;
Dimension menuSize = _popupMenu.getPreferredSiz
//get the location of the Mouse
Point absolutePosition = new Point();
SwingUtilities.convertPoin
//get screen size
Dimension screenSize = Toolkit.getDefaultToolkit(
// get menu location
Point menuLocation = evt.getPoint();
// check width
if(absolutePosition.x + menuSize.width > screenSize.width){
// adjust width
menuLocation.translate(-me
} //if
// check height
if(absolutePosition.y + menuSize.height > screenSize.height){
// adjust height
menuLocation.translate(0,-
} //if
_popupMenu.show(this, menuLocation.x, menuLocation.y);
return;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.