Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JMenuItem action Listener

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JMenuItem action Listener

    Hi, Ive been trying to add action listeners to my menu items. I've tried various methods but can't get anything to work! I'm not too familiar with extends and implements but if I was shown to add one action listener it would a massive help. thanks in advance

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

    public class wordDictionary
    {
    public static void main(String [] args)
    {
    wordDictionary aFrame = new wordDictionary();
    }

    public wordDictionary()
    {
    JFrame aFrame = new JFrame();
    Container contentPane;
    aFrame.setTitle("Word Dictionary");
    aFrame.setResizable(false);
    aFrame.setSize(400, 300);

    aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    aFrame.setVisible(true);
    JMenuBar menuBar = new JMenuBar();
    //Menu Items
    JMenu analyseText = new JMenu("Analyse Text");
    JMenu dictionary = new JMenu("Dictionary");
    JMenu statistics = new JMenu("Statistics");
    JMenu exit = new JMenu("Exit");

    // Dictionary Menu Items
    JMenuItem addWord = new JMenuItem("Add Word");
    JMenuItem deleteWord = new JMenuItem("Delete Word");
    JMenuItem searchWord = new JMenuItem("Search");
    JMenuItem listEntries = new JMenuItem("List Entries");

    //Statistic menu items
    JMenuItem wordCount = new JMenuItem("Word Count");
    JMenuItem palindromes = new JMenuItem("Palindromes");
    JMenuItem longestWord = new JMenuItem("Longest Word");
    JMenuItem shortestWord = new JMenuItem("Shortest Word");
    JMenuItem alphabeticFreq = new JMenuItem("Alphabetic Frequency");

    //add listeners
    //MenuListener listener = new MenuListener();
    //deleteWord.addMenuListener(this);

    menuBar.add(analyseText);
    menuBar.add(dictionary);
    menuBar.add(statistics);
    menuBar.add(exit);

    //Add items to dictionary menu
    dictionary.add(addWord);
    dictionary.add(deleteWord);
    dictionary.add(searchWord);
    dictionary.add(listEntries);

    //Add items to statistics menu
    statistics.add(wordCount);
    statistics.add(palindromes);
    statistics.add(longestWord);
    statistics.add(shortestWord);
    statistics.add(alphabeticFreq);



    //Set menu bar for JFrame
    aFrame.setJMenuBar(menuBar);
    }

    private class addWord extends JMenuItem implements ActionListener
    {
    public addWord(String text)

    {

    addWord.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
    System.out.println("Item clicked: "+e.getActionCommand());
    }

    }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: JMenuItem action Listener

    You'll find example code and explanation here: Swing Menu Tutorial

Similar Threads

  1. ACTION LISTENER HANDLING
    By fari in forum What's Wrong With My Code?
    Replies: 19
    Last Post: June 12th, 2010, 08:31 PM
  2. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  3. Beginner Help (Action Listener)
    By gradstudent in forum AWT / Java Swing
    Replies: 2
    Last Post: April 30th, 2010, 10:26 AM
  4. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM