JPopupMenu goes out of screen view

I have JPopupMenu which containes also submenues , when activated near the screen edge the behaviour is not correct , like it does with AWT.
I need to fix this problem under swing components.

there must be a way to overcome this behavior.
please add some source code
sadounjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
try adding
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
0
sadounjAuthor Commented:
did'nt work !!!
0
heyhey_Commented:
what is your OS / JDK version / Swing version ? post some sample code (small compilable example)
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

sadounjAuthor Commented:
Adjusted points to 125
0
sadounjAuthor Commented:
NT JDK 1.2
sample code not relevant .
it is part of  a big!! application.

I read about this problem , I'm not the first one.

I understand that it concerned with heavy component / light component issue

try this sample code which work fine.(also at the edges of the screen)

if you can transfer it to Swing Components I'll be truely happy!!

///////////////////////////////////////
import java.awt.event.*; import java.awt.*;

public class AWTPopup
{
    public static void main(String args[])
    {
        new AWTPopupFrame();
    }
}

class AWTPopupFrame extends Frame
{
    PopupMenu pm = new PopupMenu();
    Menu colors = new Menu("Colors");
    MenuItem red = new MenuItem("Red");
    MenuItem green = new MenuItem("Green");
    MenuItem blue = new MenuItem("Blue");
    MenuItem help = new MenuItem("Help");

    AWTPopupFrame()
    {
        super();

        /* Add the menu items to the menu */
        colors.add(red);
        colors.add(green);
        colors.add(blue);

        /* Add the menus to the popup menu */
        pm.add(help);
        pm.add(colors);

        /* Add the popup to the frame */
        add(pm);

        /* Add the mouse listener */
        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent evt)
            {
                if (evt.isMetaDown())
                    pm.show(AWTPopupFrame.this,evt.getX(),evt.getY());
            }
        });


        /* Add the window listener */
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent evt)
            {
                dispose();
                System.exit(0);
            }
        });

        /* Size the frame */
        setSize(200,200);

        /* Center the frame */
        //Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        //Rectangle frameDim = getBounds();
        //setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);


        /* Show the frame */
        setVisible(true);
    }
}  
////////////////////////////////////
0
heyhey_Commented:
use JFrame and JPopupMenu.
if it does not work, please post the code that does not work.
0
sadounjAuthor Commented:
import java.awt.event.*; import java.awt.*;
import javax.swing.*;

public class AWTPopup
{
    public static void main(String args[])
    {
        new AWTPopupFrame();
    }
}

class AWTPopupFrame extends JFrame
{
    JPopupMenu pm = new JPopupMenu();
    Menu colors = new Menu("Colors");
    MenuItem red = new MenuItem("Red");
    MenuItem green = new MenuItem("Green");
    MenuItem blue = new MenuItem("Blue");
    MenuItem help = new MenuItem("Help");

    AWTPopupFrame()
    {
        super();

        /* Add the menu items to the menu */
        colors.add(red);
        colors.add(green);
        colors.add(blue);

        /* Add the menus to the popup menu */
        pm.add(help);
        pm.add(colors);

        /* Add the popup to the frame */
        add(pm);

        /* Add the mouse listener */
        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent evt)
            {
                if (evt.isMetaDown())
                    pm.show(AWTPopupFrame.this,evt.getX(),evt.getY());
            }
        });


        /* Add the window listener */
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent evt)
            {
                dispose();
                System.exit(0);
            }
        });

        /* Size the frame */
        setSize(200,200);

        /* Center the frame */
        //Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        //Rectangle frameDim = getBounds();
        //setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);


        /* Show the frame */
        setVisible(true);
    }
}
0
heyhey_Commented:
DID YOU EVEN TRY TO COMPILE YOUR EXAMPLE ?


hey man, this is YOUR PROBLEM, if you want us to help you, please post at least compilable code and THROW AWAY all the AWT stuff !

I am not paid for helping people on this board. neither are the other experts.

no offence. just tired :(
0
sadounjAuthor Commented:
I'm Sorry!!!
I did Compile .
you asked to post the code that does not work. so I did!!!



Avery thing you said I tried before! with no succes.

I realy want to know how to convert the AWT example to the Swing


please!!??? )-:
)-: help me!
I dont own more points, If I had I would give more!!!
0
MurthyMvlnCommented:
To solve this problem you should check for the mouse points (on panel )with popupmenu dimensions. please follow the explanation below as mentioned.

m_workspace is a JPanel. and
JPopupMenu jpm = new JPopupMenu();
//.....
jpm.show( m_workspace,event.getX(),  event.getY() );

//Now Here you should check for the panel dimensions with popupmenu dimensions. so

Point p = getDimension(
new Point(event.getX(), event.getY()),
new Point(jpm.getWidth(), jpm.getHeight()));

if( p.x != event.getX() || p.y != event.getY() )
  jpm.show( m_workspace, p.x, p.y );



//This method to get the point where the popup menu should display. First arg is mouse clicked point on panel and second arg is popup menu dimentions.

public Point getDimension(Point p1, Point p2)
{
   int width  = this.getWidth();
   int height = this.getHeight();

if(( p1.x + p2.x > width) && (p1.y +  p2.y > height))
{
     p1.x = p1.x - p2.x ;
     p1.y = p1.y - p2.y ;
}
else if(( p1.y + p2.y > height) && (  
p1.x + p2.x < width))
{
   p1.y = (height - p2.y);
}
else if(( p1.x + p2.x > width) && ( p1.y + p2.y < height))
{
   p1.x = (width - p2.x);
}

   return p1;
}




0
heyhey_Commented:
ok
sorry.

this code works well on WinNT 2000 / IBM JDK 1.1.8 / swing-1.1.1
it should work on your platform too

import java.awt.event.*;
import javax.swing.*;

public class SwingPopup
{
    public static void main(String args[])
    {
        new SwingPopupFrame();
    }
}
class SwingPopupFrame extends JFrame
{
    JPopupMenu pm = new JPopupMenu();
    JMenu colors = new JMenu("Colors");
    JMenuItem red = new JMenuItem("Red");
    JMenuItem green = new JMenuItem("Green");
    JMenuItem blue = new JMenuItem("Blue");
    JMenuItem help = new JMenuItem("Help");
    SwingPopupFrame()
    {
        super();
        /* Add the menu items to the menu */
        colors.add(red);
        colors.add(green);
        colors.add(blue);
        /* Add the menus to the popup menu */
        pm.add(help);
        pm.add(colors);
        /* Add the popup to the frame */
//        add(pm);
        /* Add the mouse listener */
        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent evt)
            {
                if (evt.isMetaDown())
                    pm.show(SwingPopupFrame.this,evt.getX(),evt.getY());
            }
        });
        /* Add the window listener */
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent evt)
            {
                dispose();
                System.exit(0);
            }
        });
        /* Size the frame */
        setSize(200,200);
        /* Center the frame */
        //Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        //Rectangle frameDim = getBounds();
        //setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);
        /* Show the frame */
        setVisible(true);
    }
}
0
heyhey_Commented:
excuse me, if I was rude ... the main problem is that if you can't explain your problem, we are both losing time.

good question contains half of the answer :) (of course if you don't know the answer, it's not easy to ask good questions :)

enjoy Java. it's a nice language.

(and note, that this is not a question of points :)
0
sadounjAuthor Commented:
"heyhey"
thank you very much.
Sorry for being "ununderstood"

the title of the problem was:
--------------------------------------
"JPopupMenu goes out of  screen view"
--------------------------------------


by adding the AWT example I intended to ilustrates that there is no problem when you "popup" near the edges of the screen.
but the problem do occur on Swing.

your answer (I again thank you for your time), did the Conversion to Swing , but did'nt solve the problem!!!

sorry.
 
0
heyhey_Commented:
my example works fine on my system - (JPopupMenu shows, no matter where I click)

I have experienced some problens long, long ago (Swing 0.51 I think), but everything should be ok now.

did you tried my code ? did you see the same problem ?

0
sadounjAuthor Commented:
heyhey hi, & good morning

look, your code work's fine until you move the frame near the edge of the screen .
after you do that , (part of the frame is now unvisible), activate the popup menu and the submenu , and you will see that they go out of screen. and this behaviour doesn't occur in AWT.
0
heyhey_Commented:
I'm sorry :( I was addressing some slightly different problem.

AWT Popup menus behavior is controlled from your OS, and Swing JPopupMenus are implemented in pure Java ... coordinates that you pass to JPopupMenu.show() method are the desired coordinates of the upper left corner of the popupmenu.

the source:

'public void setLocation(int x, int y)
Set the location of the upper left corner of the popup menu using x, y coordinates.'

public void show(Component invoker, int x, int y) {
....
setLocation(invokerOrigin.x + x, invokerOrigin.y + y);
}

the only possible workaround is to calculate the desired x and y coordinates yourself (I don't know if it's easy or not)

best regards
  heyhey
0
sadounjAuthor Commented:
MurthyMvln:
your advise didn't work
0
sadounjAuthor Commented:
I'm sorry MurthyMvln.

doesn't work when you open the popup menu near to the screen's edge.
(try and see).


best regards.

0
heyhey_Commented:
>> the only possible workaround is to calculate the desired x and y coordinates yourself
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.