Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple but fustrating issue with swing

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Simple but fustrating issue with swing

    Im struggling to add a scrollable submenu in a pop up. I use a jPopupMenu to create the initial menu when you right click and then when you navigate to one of the options it displays a scrollpane with all my data in but when you move the mouse to the scrollpane it ditches the initial "invoking" pop up menu which inturn removes the scrollpane.

    Please help...

    import java.awt.*;  
    import java.awt.event.*;  
    import javax.swing.*;  
     
    public class ScollingSubMenu {
     
       private static javax.swing.JPopupMenu MainMenu;
       private static JPopupMenu AddButtonPopup;
     
        private static void AddButtonStateChanged(javax.swing.event.ChangeEvent evt) {
            javax.swing.JMenu temp = (javax.swing.JMenu)evt.getSource();
            if (temp.isSelected()) {
                if (!AddButtonPopup.isVisible()) {
                    Rectangle r = AddButtonPopup.getInvoker().getBounds();
                    Point p = new Point(r.x+r.width, r.y-3);
                    SwingUtilities.convertPointToScreen(p, AddButtonPopup.getInvoker().getParent());
                    AddButtonPopup.setLocation(p);
                    AddButtonPopup.setVisible(true);
                }
            }
            else {
                Rectangle r = AddButtonPopup.getBounds();
                r.x = AddButtonPopup.getLocationOnScreen().x - 5;
                r.y = AddButtonPopup.getLocationOnScreen().y - 5;
                r.height = r.height + 10;
                r.width = r.width + 10;
     
                Point mouse = MouseInfo.getPointerInfo().getLocation();
                boolean inside = r.contains(mouse);
     
                if (AddButtonPopup.isVisible()&& !inside) {
                    AddButtonPopup.setVisible(false);
                }
            }
        }
     
       private static javax.swing.JMenu TestMenu() {
            javax.swing.JMenu SizeMenu = new javax.swing.JMenu("Add Button");
            javax.swing.JMenuItem TempMenuItem;
     
            if (AddButtonPopup != null) {
                AddButtonPopup.setVisible(false);
            }
            AddButtonPopup = new JPopupMenu();
            AddButtonPopup.setInvoker(SizeMenu);        
            AddButtonPopup.setLayout(new BorderLayout());
            AddButtonPopup.setPopupSize(200, 300);
            javax.swing.JPanel panel = new javax.swing.JPanel(new GridLayout(0,1)); 
     
            SizeMenu.addChangeListener(new javax.swing.event.ChangeListener() {
                public void stateChanged(javax.swing.event.ChangeEvent evt) {
                    AddButtonStateChanged(evt);
                }
            });
     
            for (int i = 1; i < 60; i++) {
                TempMenuItem = new javax.swing.JMenuItem("Item" + String.valueOf(i));
                TempMenuItem.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Got It!");
                }
            });
                panel.add(TempMenuItem);
            }
     
            javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(panel);
            AddButtonPopup.add(scrollPane);
     
     
            return SizeMenu;
        }
     
        public static javax.swing.JPopupMenu CreateOptionsMenu() {
            MainMenu = new javax.swing.JPopupMenu();
            javax.swing.JMenuItem extraButton = new javax.swing.JMenuItem("Extra Button");
            MainMenu.add(TestMenu());
            MainMenu.add(extraButton);
     
            return MainMenu;
        }
     
        private static void jPanel1MouseReleased(java.awt.event.MouseEvent evt) {                                      
            if (evt.getButton()==3) {
                javax.swing.JPopupMenu aggregateMenu = CreateOptionsMenu();
                aggregateMenu.show(evt.getComponent(),evt.getX(),evt.getY());
            }
        }    
     
        public static void main(String[] args) {  
            JFrame f = new JFrame();  
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            f.setLocation(200,200);
            f.setVisible(true);
            javax.swing.JPanel panel1 = new javax.swing.JPanel();
            panel1.setPreferredSize(new Dimension(100,100)); 
            panel1.setLocation(200,200); 
            panel1.setVisible(true); 
            panel1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    jPanel1MouseReleased(evt);
                }
            });
            f.add(panel1);
            f.pack();        
        }  
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Simple but fustrating issue with swing

    I'm not sure I understand entirely what you are describing (pictures or something can be helpful when you are facing GUI related problems since it is hard to visually imagine someone else's GUI design with just the code), but if I think I understand you correct, could it be that Swing does not allow two light-weight popup components at the same time? (a simple example would be: imagine a JComboBox inside a JPopupMenu. If you open the JPopupMenu and attempt to then open the JComboBox in the menu, the menu will close)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple but fustrating issue with swing

    http://www.beatricehaines.com/screen.jpg

    Here is an image. The left popupmenu appears when you right click and then when you move the mouse over the add button the scrollpane appears in place but when you move the mouse over to the scroll pane the left menu vanishes and takes the right one with it (i guess its parent goes so it follows).

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Simple but fustrating issue with swing

    Yeah, I don't think Swing supports the behavior you want. Your JPanel (inside of the scroll panel) is a lightweight container, as is your popup menu. When you attempt to focus on your JPanel, it automatically closes the popup menu and, as you mentioned, when the popup menu goes, so does your JPanel. Have you considered simply making the Add Button item in your popup menu open a JWindow or JDialog which contains your list and scroll pane?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Swing, JFrame simple questions!
    By jaw88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 22nd, 2013, 06:34 PM
  2. JAVA SWING ISSUE
    By syedghouse14 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 11:28 AM
  3. Don't know how to do make this works: SWING issue
    By hamisto90 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 6th, 2012, 08:20 AM
  4. [SOLVED] Simple calculator issue
    By ikocijan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2012, 02:43 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM