• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPopupMenu

 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JPopupMenu in my application to show the right-click popup menu.I'm doing it like this:
popupMenu.show(e.getComponent(),e.getX(),e.getY());
The popup menu shows at that point and all popupmenu items appear below that point. This causes problems when i right-click at the bottom of my screen. Here some of the popupmenu components become invisible. So, i need a way to display the popup menu items upwards from that point.
Is there a way to achieve that ?
Thanks,
- Raja.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing forum.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that it's the user's fault for clicking too close to the bottom of the screen, but I'm sure you could override the show() method to do a test of the JPopupMenu's getBounds() against the bounds of the screen ( Toolkit.getDefaultToolkit().getScreenSize() ) and shift the JPopup appropriately with JPopup.setLocation().

-Nate
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think you didn't understand my question.
setLocation() will shift the bounds of the JPopupMenu. When a user clicks on a table row, popup menu should appear at that point and not in some other place.
Normally, When we do right-click, menu items appear at bottom-right. Is there a way to display it like top-right or top-left or bottom-left ? When you right-click on the ends of Internet Explorer or Windows desktop you can see that popup menu changes it's direction. (location is not changed here only direction is changed)
Is there a way to achieve this in Java?
Thanks,
- Raja.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I don't think you understood my answer...

If you detect the mouse click being within a certain range of the edge of the screen, you will recalculate the location the JPopup menu will appear... the location is always the upper left corner... if you want the JPopup to appear with its lower left corner where the arrow was, you'll have to calculate the new location of the upper left corner... something like the following...



This is the general idea...

You may have to mess with screen vs. component coordinates to get this to work, but I think it should work as illustrated above...

-Nate
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan, That works fine. I didn't understand it first time. But i think it has a small problem.
popupMenu.getHeight() returns 0 in my show() method. It returns the actual height only after i call super.show(). So, in my application i'm calling popuMenu.show() twice so that after first time it gets the popuMenu height and during second time it displays it properly.
Since i'm it displaying the popuup menu twice, it causes slight flickering. Is there a way to avoid this?
Thanks,
- Raja.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops... use getPreferredSize().width instead of getWidth() and getPreferredSize().height instead of getHeight(). getPreferredSize() should return the size the component should be even if the component is not being displayed... getWidth() and getHeight() return the size the component actually is (which, if the component is not visible, is 0).

-Nate
 
Raja Kannappan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan,
That works fine. Thanks a lot for your help!
- Raja.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!