Question

Any idea how I can show many pop-up menus at the same time in a good way?(for JPopupMenu) I tried to @Override show(Component invoker, int x, int y) and managed to make many visible in the same time by removing setInvoker(invoker);. Problem with this is that I dont manage to remove the popups in any way.

Question: Any idea how to make JPopupMenus still be visible when more JPopupMenus are shown but work as usual otherwise(close/hide for other actions)?

public class MultiPopupMenu {

    public static void main(String[] args){

        // Create popup
        JPopupMenu menu1 = createPopupMenu("First label");
        JPopupMenu menu2 = createPopupMenu("Second label");

        // Create labels
        JLabel label1 = new JLabel("abcde");
        JLabel label2 = new JLabel("1234");

        JPanel panel = new JPanel();
        panel.add(label1);
        panel.add(label2);

        // Add labels
        JFrame frame = new JFrame();
        frame.add(panel);

        frame.setPreferredSize(new Dimension(200,100));
        frame.pack();
        frame.setVisible(true);

        // Show popups
        menu1.show(label1,-40,20); // Not showing
        menu2.show(label2, 0,20);
    }

    private static JPopupMenu createPopupMenu(String label){
        JPopupMenu popup = new JPopupMenu();
        JLabel lblTest = new JLabel(label);
        popup.add(lblTest);
        popup.setBackground(Color.YELLOW);
        return popup;
    }
}
Was it helpful?

Solution

  • in current Swing isn't possible to show two lightweight popup containers in the same time, second popup container hide() first immmediatelly (changes/since in/from Java4)

  • create JWindow (JTextComponents aren't editable) or undecorated JDialog and to override

    1. setVisible for escape key (add KeyBindings) and focusLost / (better) WindowFocusListener

    2. add there JPanel with JButtons (firing setVisible as 1st code line, rest is wrapped into invokeLater, delayed by invokeLater)

    3. then you can to put JComboBox as JMenuItem (not possible see description in my 1st. sentence)

OTHER TIPS

public class MultiPopupMenu {

    public static void main(String[] args){

        // Create popup
        JWindow popup1 = createPopup("First label");
        JWindow popup2 = createPopup("Second label");

        // Create labels
        JLabel label1 = new JLabel("abcde");
        JLabel label2 = new JLabel("1234");

        JPanel panel = new JPanel();
        panel.add(label1);
        panel.add(label2);

        // Add labels
        JFrame frame = new JFrame();
        frame.add(panel);

        frame.setPreferredSize(new Dimension(200,100));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        // Show popups
        popup1.pack();
        popup2.pack();

        Point floc = frame.getLocation();
        Point loc = label1.getLocation();
        System.out.println(floc);
        popup1.setLocation((int)(floc.getX()+loc.getX())-20, (int)(floc.getY()+loc.getY())+40);
        loc = label2.getLocation();
        popup2.setLocation((int)(floc.getX()+loc.getX())+20, (int)(floc.getY()+loc.getY())+40);

        popup1.setBackground(Color.YELLOW);
        popup1.setVisible(true);
        popup2.setVisible(true);
    }

    private static JWindow createPopup(String label){
        JWindow popup = new JWindow();
        JLabel lblTest = new JLabel(label);
        popup.add(lblTest);
        popup.getContentPane().setBackground(Color.YELLOW);
        return popup;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top