display dialog box by clicking a menu

Posted on 2006-04-02
Last Modified: 2010-03-31
Dear experts,

Is there a way to display a dialog box by clicking an option from a menu?

thank you
Question by:Halt123
    LVL 8

    Assisted Solution


    Author Comment

    I've had a look through here but I am still unsure how to do it
    LVL 92

    Assisted Solution

    In the action listener for the menu use:

    JOptionPane.showMessageDialog(this, "my message");

    some more info:
    LVL 8

    Assisted Solution

    LVL 23

    Assisted Solution

    LVL 8

    Accepted Solution

    In the constructor for your code, you'll ned something like this. This just constructs the menu and adds an action listsner to the menu item

    // Construct the menu:
    JMenu menu = new JMenu();
    // construct the menu items
    JMenuItem item = new JMenuItem();

    The class in which this code is contained must implement ActionListsner's actionPerformed() method:

    public void actionPerformed(ActionEvent e){
         if (e.getSource() instanceof JMenuItem)
              JOptionPane.showMessageDialog(this, "my message");


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    canBalance challenge 34 55
    What is the latest versions eclipse neon 2 55
    Starting to learn JAVA, 7 34
    solarwind tftp server 2 16
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now