Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By Huskies

Thread: How can you add an action listener to a menu item?

  1. #1
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Question How can you add an action listener to a menu item?

    Here is a little snippet of my code:

    ______________________[starts here]________________________
    Java Code:
    public class MyCopyFile extends JFrame implements ActionListener {
    	private JButton jbtCopy = new JButton("Copy");
    	private JTextField jtfSrce = new JTextField();
    	private JTextField jtfDest = new JTextField();
    	public static void main(String[]args) {
    		MyCopyFile frame = new MyCopyFile();
    		frame.setTitle("FileCopy");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		frame.pack();
    		JMenuBar jmb = new JMenuBar();
    		frame.setJMenuBar(jmb);
    		JMenu helpMenu = new JMenu("Help");
    		jmb.add(helpMenu);
    		JMenuItem ins = new JMenuItem("Instructions");
    		helpMenu.add(ins);
    		ins.addActionListener(this);*
    	}
    	public MyCopyFile() {
    		Container container = getContentPane();
    		container.setLayout(new BorderLayout());
    		JPanel p = new JPanel();
    		JPanel p1 = new JPanel();
    		p.setLayout(new GridLayout(2,2));
    		p.add(new Label("Source Directory:"));
    		p.add(jtfSrce);
    		p.add(new Label("Destination Directory"));
    		p.add(jtfDest);
    		p1.setLayout(new BorderLayout());
    		p1.add(jbtCopy, BorderLayout.CENTER);
    		container.add(p, BorderLayout.NORTH);
    		container.add(p1, BorderLayout.SOUTH);
    		jbtCopy.addActionListener(this);
    	}
    ______________________[Ends Here]________________________

    I have placed an asterix next to where the problem lies. I have added a menu bar to the frame. I have then added a menu help to this. And then added a menu item ins to the help menu. I wish to add an action listener to ins. In eclipse it comes up with an error saying that it cannot be used in a static context. How do i solve this? Thanks in advance.
    Last edited by Norm; 08-24-2011 at 07:44 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You should really be making the components instance variables rather than creating them in main. If you are making this menu item in main, how can you access it from methods outside of main?(actionPerformed for example). You should be doing very little work inside the main method.

  3. #3
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    You should just get the program running in main, but do most of the work outside of it. What i would do (what i do) is make a new class that actually makes the window, and then make an inner class that implements ActionListener.

    then you can add the actionListener to your help MenuItem with something like

    Java Code:
    public class WindowFactory{
    	public void createWindow(){
    		//make window
                    //make button h
                    h.addActionListener(new listener());
    	}
    
    	private class Listener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			//do stuff when activated
    		}
    	}
    }
    Last edited by Huskies; 08-24-2011 at 05:59 PM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please use correct naming conventions when giving advice. In classes the first letter of each word should be capitalized. WindowFactory, not windowFactory, and Listener, not listener. Method names get the format you used(first letter first word lowercase, the first letter of all subsequent words capitalized)

  5. #5
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    @sunde sorry, i always mix that up. Is that better?

  6. #6
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Huskies View Post
    You should just get the program running in main, but do most of the work outside of it. What i would do (what i do) is make a new class that actually makes the window, and then make an inner class that implements ActionListener.

    then you can add the actionListener to your help MenuItem with something like

    Java Code:
    public class WindowFactory{
    	public void createWindow(){
    		//make window
                    //make button h
                    h.addActionListener(new listener());
    	}
    
    	private class Listener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			//do stuff when activated
    		}
    	}
    }
    So what should i include within my main method? And how should i call the class "WindowFactory" inside my main method? Where do i add things to my frame and to my container? Sorry for taking up so much of your time.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    You could change your code by moving a few lines:
    Java Code:
    	public static void main(String[]args) {
    		MyCopyFile frame = new MyCopyFile();
            } // move this here and start the constructor on following:
            public MyCopyFile() {
               JFrame frame = this; // Q&D to get following to compile. Really remove frame. from following
    		frame.setTitle("FileCopy");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		frame.pack();
    		JMenuBar jmb = new JMenuBar();
    		frame.setJMenuBar(jmb);
    		JMenu helpMenu = new JMenu("Help");
    		jmb.add(helpMenu);
    		JMenuItem ins = new JMenuItem("Instructions");
    		helpMenu.add(ins);
    		ins.addActionListener(this);
    //	}
    //	public MyCopyFile() {
    SerbianSergeant likes this.

  8. #8
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    so in your main method you create (forgive my lack of java terms...so i guess this is a question for norm and sunde) object, and then run the methods that are associated with it. so your main would look like this, in the example i gave earlier.

    Java Code:
    public static void main(String[]args) {
    		WindowFactory window = new WindowFactory();
                    window.createWindow();
            }

    this will create a new "WindowFactory" object and then run the createWindow method. Somebody correct me if my terminology is incorrect
    SerbianSergeant likes this.

Similar Threads

  1. Error when clicking a menu item JSF + Richfaces
    By pepelegal in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-24-2011, 11:03 PM
  2. Replies: 0
    Last Post: 07-13-2010, 09:16 AM
  3. jcheckbox item listener
    By GUIJAVA in forum New To Java
    Replies: 2
    Last Post: 02-09-2010, 11:15 AM
  4. Checking toggle state of a menu item
    By xsive in forum SWT / JFace
    Replies: 1
    Last Post: 09-22-2008, 02:42 PM
  5. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •