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(); } }