?
Solved

JPopupMenu goes out of  screen view

Posted on 2000-01-31
19
Medium Priority
?
385 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:sadounj
  • 9
  • 9
19 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2459665
try adding
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
0
 

Author Comment

by:sadounj
ID: 2459928
did'nt work !!!
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2460342
what is your OS / JDK version / Swing version ? post some sample code (small compilable example)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:sadounj
ID: 2460841
Adjusted points to 125
0
 

Author Comment

by:sadounj
ID: 2460842
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2461203
use JFrame and JPopupMenu.
if it does not work, please post the code that does not work.
0
 

Author Comment

by:sadounj
ID: 2461321
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2461483
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
 

Author Comment

by:sadounj
ID: 2461602
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
 

Expert Comment

by:MurthyMvln
ID: 2462103
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2462702
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2462746
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
 

Author Comment

by:sadounj
ID: 2462921
"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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2464870
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
 

Author Comment

by:sadounj
ID: 2476994
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2477380
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
 

Author Comment

by:sadounj
ID: 2477421
MurthyMvln:
your advise didn't work
0
 

Author Comment

by:sadounj
ID: 2477667
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
 
LVL 16

Accepted Solution

by:
heyhey_ earned 360 total points
ID: 2481343
>> the only possible workaround is to calculate the desired x and y coordinates yourself
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month4 days, 11 hours left to enroll

601 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