Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

popup menu goes out over screen size

Posted on 2004-10-25
10
Medium Priority
?
408 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:greg_100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12401224
0
 
LVL 92

Expert Comment

by:objects
ID: 12405154
> 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.
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 12408096
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.getLocationOnScreen();
            Dimension preferredSize = getPreferredSize();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     
            // 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.



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:grim_toaster
ID: 12408112
Or if you are having dificulties with proper drop-down menues, then create a custom implementation of JMenu...
0
 

Author Comment

by:greg_100
ID: 12422342
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 12428239
possibly an error in your calcs, how exactly is it not working?
0
 

Author Comment

by:greg_100
ID: 12433888
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.
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 12434350
Could you provide the code that you are using, then we may be able to help you some more...
0
 

Author Comment

by:greg_100
ID: 12436446
// get menu size
      Dimension menuSize = _popupMenu.getPreferredSize();

      //get the location of the Mouse
      Point absolutePosition = new Point();
      SwingUtilities.convertPointToScreen(absolutePosition, evt.getComponent());

      //get screen size
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

      // get menu location
      Point menuLocation = evt.getPoint();

      // check width
      if(absolutePosition.x + menuSize.width > screenSize.width){

        // adjust width
        menuLocation.translate(-menuSize.width,0);

      } //if

      // check height
      if(absolutePosition.y + menuSize.height > screenSize.height){

        // adjust height
        menuLocation.translate(0,-menuSize.height);

      }  //if

      _popupMenu.show(this, menuLocation.x, menuLocation.y);

      return;
      
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 12439625
you need to convert the mouse coords:

 Point mouse = evt.getPoint();
 SwingUtilities.convertPointToScreen(mouse, evt.getComponent());

and use that in your checks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question