-2

I'm trying to show a pop-up window when the user clicks on a JMenuItem. I've got the following piece of code:

menuAnular = new JMenuItem(w.translate("ETI_ANULAR"),imageAnular);
menuAnular.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        JDialog yesNo = new JDialog();
        yesNo.setVisible(true);
    }

});

Sadly, with this lines nothing is showed up. I've tried several ways to do it but I can't see nothing displayed.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
gab
  • 29
  • 5
  • can we see the custom JDialog class? – ΦXocę 웃 Пepeúpa ツ May 10 '17 at 07:39
  • JDialog is a Class contained in the Swing library from javax. Here's the documentation: https://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.html – gab May 10 '17 at 07:43
  • 1
    sorry, but I can not find in the official (doc)[https://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.html#JDialog()] you posted a constructor as ***JDialog(String x)***, can we see the custom JDialog class? – ΦXocę 웃 Пepeúpa ツ May 10 '17 at 07:46
  • Sorry, my mistake, I forgot to erase the String from the parameters. Asume my constructor is JDialog() without params. – gab May 10 '17 at 07:48
  • Probably you've forgotten to make your dialog modal? Try to add `yesNo.setModal(true);` before the line `yesNo.setVisible(true);` – Sergiy Medvynskyy May 10 '17 at 07:49
  • your code should work...is the addActionListener getting trggered? – ΦXocę 웃 Пepeúpa ツ May 10 '17 at 07:51
  • 1
    Consider providing a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) so we can skip the guessing game – MadProgrammer May 10 '17 at 07:52
  • I tested your code. It works. The problem must be somewhere in the code you didn't post. Add a message to the actionPerformed-method to check if it gets invoked (or activate your debugger) – Tobi May 10 '17 at 07:53
  • Oh, that advice about setModal() worked. Until now, I hadn't never used that inherited method... can I ask what is it used for? – gab May 10 '17 at 07:55
  • Your code should work as posted. setModal() is obsolete and should not be used. Use setModalityType. See javadoc. – Tobi May 10 '17 at 08:03
  • 1
    *"can I ask what is it used for?"* Get it straight from the horses' mouth. 1) Visit the [`JDialog`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JDialog.html) Java Docs. 2) Find `setModal` in the page. 3) You might notice this method is inherited from `java.awt.Dialog` 4) Click the link. 5) **Read.** - Note that SO is not a substitute for reading the documentation. That documentation is vital for coding Java. Although I use an IDE that has pop-ups linked to the [Java Docs](https://docs.oracle.com/javase/7/docs/api/), I ***always*** have them open in a tab of the browser. – Andrew Thompson May 10 '17 at 08:16

1 Answers1

0
menuAnular.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                final JPopupMenu popup = new JPopupMenu();
                popup.add(new JMenuItem(new AbstractAction("TITLE OF THE NEW JMENUITEM OF THE POPUP") {
                    public void actionPerformed(ActionEvent e) {
                         if (JOptionPane.showConfirmDialog(contentPanel, 
                                "Dialog", "Dialog title", 
                                JOptionPane.YES_NO_OPTION,
                                JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
                         {
                           //DO SOMETHING
                         }
                    }
                }));
popup.show(e.getComponent(), e.getX(), e.getY());
            }});

The new POP-UP will be created in the place in which the user have clicked.

The object 'contentPanel' is the main JPanel of your application.

I hope this help.

AngryCoder
  • 396
  • 3
  • 15