Solved

Positionin a popup menu

Posted on 1998-02-20
3
255 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:msmolyak
[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
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
gwalters earned 200 total points
ID: 1233171
Dimension sz=menu.size();
Dimension scrsz=Toolkit.getDefaultToolkit.getScreenSize();
Point loc=CURSOR_LOCATION;
menu.move(Math.max(loc.x,scrsz.width-sz.width),Math.max(loc.y,scrsz.height-sz.height));




I put a holder for CURSOR_LOCATION because, without your code, I didn't know how you would get that.

Basically, the x,y coordinate of the click is the preferred location.  Move it up or to the left if putting it there would cause it to be offscreen.
0
 
LVL 3

Expert Comment

by:gwalters
ID: 1233172
Oops, those should be Math.min, not Math.max!

sorry.
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1233173
I slightly modified you logic to come up with

// Position the menu on the screen
Dimension sz=menu.size();
Dimension scrsz=Toolkit.getDefaultToolkit().getScreenSize();
if (scrsz.width - (parentFrame.getLocationOnScreen().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.getLocationOnScreen().x + x) - sz.width;
}
else
{
    // Position the menu to the right of the cursor
    x = parentFrame.getLocationOnScreen().x + x + 10;
}
y = Math.min(parentFrame.getLocationOnScreen().y + y + 25,
             scrsz.height-sz.height);

this.menu.setLocation(x,y);

Thanks for your help.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

729 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