Hey;

I am following CoreJava and came to Event Handling chapter. I am trying to show a popup menu when the red button is clicked, but not works. Background color changes but no popup menu is shown. What is the problem?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package corejava_eventhandling;

import java.awt.EventQueue;
import javax.swing.JFrame;

/**
 *
 * @author melt
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                Frame frame = new Frame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        }

                );
    }

}
package corejava_eventhandling;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Frame extends JFrame
{
    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 200;
    private JPanel panel;
    public Frame()
    {
        setTitle("Button Test");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        JButton yellowButton    = new JButton("Yellow");
        JButton blueButton      = new JButton("Blue");
        JButton redButton       = new JButton("Red");

        panel = new JPanel();

        panel.add(yellowButton);
        panel.add(blueButton);
        panel.add(redButton);

        add(panel);

        ColorAction yellowAction    = new ColorAction(Color.YELLOW);
        ColorAction blueAction      = new ColorAction(Color.BLUE);
        ColorAction redAction       = new ColorAction(Color.RED);
        Panic panicAction           = new Panic();
        yellowButton.addActionListener(yellowAction);
        blueButton.addActionListener(blueAction);
        redButton.addActionListener(redAction);
        redButton.addActionListener(panicAction);

    }

    private class Panic implements ActionListener
    {
        private String message = "Warning ! Nuclear Missile Launched!!!";

        public void actionPerformed(ActionEvent event)
        {
            
            PopupMenu pop = new PopupMenu(message);
            Frame.this.panel.add(pop);
            panel.setVisible(true);
            
            
        }
    }
    private class ColorAction implements ActionListener
    {
        private Color backgroundColor;
        public ColorAction(Color c)
        {
            backgroundColor = c;
        }

        public void actionPerformed(ActionEvent event)
        {
            panel.setBackground(backgroundColor);
        }
    }
}

Dani AI

Generated

The popup did not appear because creating and adding an AWT PopupMenu to the panel does not automatically display it. As pointed out, you must call the popup's show(...) method to make it visible; confirmed that using show fixed the immediate problem.

A better, more robust choice for Swing applications is JPopupMenu (it is lightweight and integrates with Swing painting and layout). JPopupMenu also exposes the familiar show(invoker, x, y) API and can be attached with setComponentPopupMenu(...) for automatic behavior. See the JPopupMenu docs and the Swing popup-menu tutorial for details:

Quick examples (not present in the original posts):

JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Abort"));
// inside an ActionListener (no mouse coords available)
popup.show(redButton, 0, redButton.getHeight());

To show a popup at the mouse position (recommended for right-click menus), use a MouseListener and isPopupTrigger() (check both press and release for cross-platform correctness):

redButton.addMouseListener(new MouseAdapter() {
    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
});

Troubleshooting tips: show the popup from the Event Dispatch Thread (your invokeLater is correct), make sure the invoker component is visible, and avoid mixing AWT PopupMenu with Swing components to prevent z-order/painting issues.

Recommended Answers

All 2 Replies

Go read some more about using the PopupMenu class. You need to use the show method.
Perhaps a search on a java forum would find some code samples.

Yes, show method made it visible. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.