Link to home
Start Free TrialLog in
Avatar of sadounj
sadounj

asked on

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
Avatar of heyhey_
heyhey_

try adding
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
Avatar of sadounj

ASKER

did'nt work !!!
what is your OS / JDK version / Swing version ? post some sample code (small compilable example)
Avatar of sadounj

ASKER

Adjusted points to 125
Avatar of sadounj

ASKER

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);
    }
}  
////////////////////////////////////
use JFrame and JPopupMenu.
if it does not work, please post the code that does not work.
Avatar of sadounj

ASKER

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);
    }
}
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 :(
Avatar of sadounj

ASKER

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!!!
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;
}




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);
    }
}
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 :)
Avatar of sadounj

ASKER

"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.
 
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 ?

Avatar of sadounj

ASKER

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.
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
Avatar of sadounj

ASKER

MurthyMvln:
your advise didn't work
Avatar of sadounj

ASKER

I'm sorry MurthyMvln.

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


best regards.

ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial