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 5 of 5

Thread: Menu Event Listeners

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Menu Event Listeners

    I follows this. javax.swing.event.MenuListener - Menu Listener Interface


    And i keep getting this error. Note, i have not added it to the menu objects.

    Sorry for the long code guys!

    package interfacepkg;
     
     
    import javax.swing.*;
    import javax.swing.event.*;
     
    import java.awt.event.*;
    import java.awt.*;
    /**
     *
     * @author Josh Kruger
     */
    public class Main extends JFrame implements MenuListener{
     
       JTextField ClientName = new JTextField(20);
       JTextField DOB = new JTextField(9);
       JButton ok = new JButton("Ok");
       JButton Can = new JButton("Cancel");
     
     
       //--------------------------------------------------------------
     
       JMenuBar Tools = new JMenuBar();
     
     
     
       JMenu AddClients = new JMenu("Add Clients");
       JMenuItem addone = new JMenuItem("Add a Client");
     
     
       JMenu EditClients = new JMenu("Edit Clients");
       JMenuItem PickCurrent = new JMenuItem("Choose Client");
       JMenuItem EditHim = new JMenuItem("Edit Choosen Client");
       JMenuItem RemoveC = new JMenuItem("Remove a Client");
     
       JMenu Fresh = new JMenu("Advanced");
       JMenuItem reset = new JMenuItem("Reset Data");
       JMenuItem restart = new JMenuItem("Re-Build Pages");
     
     
     
       //-----------------------------------------------------------------
       public void Window1()
       {
     
     
           setTitle("Test Interface");
           setSize(400,150);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
           AddClients.add(addone);
     
     
           EditClients.add(PickCurrent);
     
           EditClients.add(EditHim);
     
           EditClients.addSeparator();
           EditClients.add(RemoveC);
     
     
           Fresh.add(reset);
     
           Fresh.addSeparator();
           Fresh.add(restart);
     
     
           Tools.add(AddClients);
           Tools.add(EditClients);
           Tools.add(Fresh);
     
           setJMenuBar(Tools);
     
           GridLayout gridall = new GridLayout(3,3,5,8);
           JPanel pane = new JPanel();
           pane.setLayout(gridall);
     
     
           JLabel Name = new JLabel("Name: ");
           JLabel Date = new JLabel("Date of Birth");
     
           pane.add(Name);
           pane.add(ClientName);
           pane.add(Date);
           pane.add(DOB);
     
           pane.add(ok);
           pane.add(Can);
     
           add(pane);
           setVisible(true);
       }
     
       public void menuSelected(MenuEvent e) {
          JMenu myMenu = (JMenu) e.getSource();
          System.out.println("Menu Selected: "+myMenu.getText());
       }
     
     
     
     
       public void actionPeformed(ActionEvent evt)
       {
           Object source = evt.getSource();
     
           if(source == addone)
           {
               setTitle("addone worked");
           }
           repaint();
     
       }
     
     
        public static void main(String[] args) {
     
     
            Main Start = new Main();
            Start.Window1();
     
            // TODO code application logic here
        }
     
    }

    This is the error i get... (In the book java6 in 21 days, his examples, as well as the link implement fine)
    java.lang.ExceptionInInitializerError
    Caused by: java.lang.RuntimeException: Uncompilable source code -[B] interfacepkg.Main is not abstract and does not override abstract method menuCanceled(javax.swing.event.MenuEvent) in javax.swing.event.MenuListener[/B]
            at interfacepkg.Main.<clinit>(Main.java:18)
    Could not find the main class: interfacepkg.Main.  Program will exit.
    Exception in thread "main" Java Result: 1


    Thanks guys!!!

  2. The Following User Says Thank You to Gondee For This Useful Post:

    javapenguin (June 16th, 2010)


  3. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Menu Event Listeners

    Well, for one, it means that your need a method menuCanceled(javax.swing.event.MenuEvent) in javax.swing.event.MenuListener

    Just like if you have your class do this:

    public class yourClass extends someClass implements ActionListener

    // lacks method actionPerformed.

    Will cause message saying that it isn't abstract and bla bla bla.

    Java 2 Platform SE v1.3.1: Interface MenuListener

    You need to define all of those methods or else you can't implement MenuListener.

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Menu Event Listeners

    package interfacepkg;
     
     
    import javax.swing.*;
    import javax.swing.event.*;
     
    import java.awt.event.*;
    import java.awt.*;
    /**
     *
     * @author Josh Kruger
     */
    public class Main extends JFrame implements MenuListener
    { // beginning of class
     
       JTextField ClientName = new JTextField(20);
       JTextField DOB = new JTextField(9);
       JButton ok = new JButton("Ok");
       JButton Can = new JButton("Cancel");
     
     
       //--------------------------------------------------------------
     
       JMenuBar Tools = new JMenuBar();
     
     
     
       JMenu AddClients = new JMenu("Add Clients");
       JMenuItem addone = new JMenuItem("Add a Client");
     
     
       JMenu EditClients = new JMenu("Edit Clients");
       JMenuItem PickCurrent = new JMenuItem("Choose Client");
       JMenuItem EditHim = new JMenuItem("Edit Choosen Client");
       JMenuItem RemoveC = new JMenuItem("Remove a Client");
     
       JMenu Fresh = new JMenu("Advanced");
       JMenuItem reset = new JMenuItem("Reset Data");
       JMenuItem restart = new JMenuItem("Re-Build Pages");
     
     
     
       //-----------------------------------------------------------------
       public void Window1()
       { // beginning of method
     
     
           setTitle("Test Interface");
           setSize(400,150);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
           AddClients.add(addone);
     
     
           EditClients.add(PickCurrent);
     
           EditClients.add(EditHim);
     
           EditClients.addSeparator();
           EditClients.add(RemoveC);
     
     
           Fresh.add(reset);
     
           Fresh.addSeparator();
           Fresh.add(restart);
     
     
           Tools.add(AddClients);
           Tools.add(EditClients);
           Tools.add(Fresh);
     
           setJMenuBar(Tools);
     
           GridLayout gridall = new GridLayout(3,3,5,8);
           JPanel pane = new JPanel();
           pane.setLayout(gridall);
     
     
           JLabel Name = new JLabel("Name: ");
           JLabel Date = new JLabel("Date of Birth");
     
           pane.add(Name);
           pane.add(ClientName);
           pane.add(Date);
           pane.add(DOB);
     
           pane.add(ok);
           pane.add(Can);
     
           add(pane);
           setVisible(true);
       } // end of method
     
       public void menuSelected(MenuEvent e) {
          JMenu myMenu = (JMenu) e.getSource(); 
          System.out.println("Menu Selected: "+myMenu.getText());
       }
     
    public void menuDeselected(MenuEvent e) {
    // put code in here
    } 
     
    public void menuCanceled(MenuEvent e){
    // put code in here
    }
     
     
     
       public void actionPeformed(ActionEvent evt)
       {
           Object source = evt.getSource();
     
           if(source == addone)
           {
               setTitle("addone worked");
           }
           repaint();
     
       }
     
     
        public static void main(String[] args) {
     
     
            Main Start = new Main();
            Start.Window1();
     
            // TODO code application logic here
        }
     
    }

    This might help some.

  5. #4
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Menu Event Listeners

    JP if thanks were a stock you'd be crashing the market at your rate .

    But anyway onto the problem you need to override all the methods of the implemented class MenuListener like so

    		public void menuCanceled(MenuEvent e) 
    	{
    		//Your code here or just leave blank
    	}
     
     
     
    	public void menuDeselected(MenuEvent e) 
    	{
    		//Your code here or just leave blank
    	}

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Menu Event Listeners

    Well, I was having problems trying to figure out JMenu, and hsi code might work if it fixed that problem, so maybe I could study it and figure out those tricky JMenus. It looks like they need menuitems and lots of listeners.

Similar Threads

  1. Action Listeners and Key Listeners Help
    By xctive in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 09:27 AM
  2. [SOLVED] JRadioButton Event Handling Help?
    By CS313e in forum AWT / Java Swing
    Replies: 2
    Last Post: March 27th, 2010, 11:56 AM
  3. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM
  4. JButton event handling.
    By Prof in forum AWT / Java Swing
    Replies: 6
    Last Post: February 3rd, 2010, 10:29 AM
  5. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM