Search code examples
javaswingactionlistenerjoptionpanejmenu

JOptionPane not showing up (null parameter, called from inner class)


I am completely new to JAVA, I am trying to make a simple application and there is no way I can get my JOptionPane to show correctly and I guess I am missing something:

here's the code:

import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Frame  extends JFrame
{       
    private static final long serialVersionUID = 1L;
    final static int WIDTH = 400;
    final static int HEIGHT = 400;

    public Frame()
    {
        super("Convert to Dxf alpha ver. - survey apps 2014");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setResizable(false);
        setLocation(100, 100);
        setBackground(Color.WHITE);
        setVisible(true);
        WelcomePanel welcomePanel = new WelcomePanel(this);
        add(welcomePanel);          
    }

    public void createMenuPanel()
    {
        MenuPanel menu = new MenuPanel();
        setJMenuBar(menu.createMenu(this));
    }
}

class MenuPanel extends JPanel implements ActionListener
{
    private JMenuItem open,save,close;
    private JMenu file,about;
    private Frame frame;
    private static final long serialVersionUID = 1L;

    public JMenuBar createMenu(Frame frame)
    {
        this.frame = frame;
        JMenuBar menuBar = new JMenuBar();          
        file = new JMenu("File");
        about = new JMenu("About");         
        menuBar.add(file);
        menuBar.add(about);         
        open = new JMenuItem("Open");
        save = new JMenuItem("Save");
        close = new JMenuItem("Close");

        file.add(open);
        file.add(save);
        file.addSeparator();
        file.add(close);

        open.addActionListener(this);
        save.addActionListener(this);
        close.addActionListener(this);
        about.addActionListener(this);

        return menuBar;
    }

    public MenuPanel()
    {
        setVisible(true);
        setOpaque(true);
        setBackground(Color.WHITE);
        setSize(Window.WIDTH,Window.HEIGHT);
    }   

    @Override
    public void actionPerformed(ActionEvent e)
    {
    Object source = e.getSource();

        if(source == open)
        {
            frame.dispose();
        }

        if(source == save)
        {           
        }

        if(source == close)
        {       
        }
        if(source == about)
        {
            JOptionPane.showMessageDialog(frame, "EasyDxfCreator - alpha version");
        }
    }

}

(I skipped the WelcomeFrame because it's just like a welcome screen that disappears after a mouse click)


Solution

  • JMenu does not operate in that way. TO get JMenu Events you need to implement MenuListener instead of ActionListener. ActionListener is good for JMenuItem.

    Hope this helps.