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

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.