Solved

popup menu goes out over screen size

Posted on 2004-10-25
407 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
Question by:greg_100
    10 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    0
     
    LVL 92

    Expert Comment

    by:objects
    > 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
    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
     
    LVL 7

    Expert Comment

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

    Author Comment

    by:greg_100
    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
    possibly an error in your calcs, how exactly is it not working?
    0
     

    Author Comment

    by:greg_100
    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
    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
    // 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:
    you need to convert the mouse coords:

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

    and use that in your checks
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    tripleUp challenge 7 47
    Connect to IP Camera using Java 4 36
    scoresIncreasing challenge 10 38
    JVM encoding. How to change encoding. 27 40
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now