Results 1 to 9 of 9
  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default JMenuItems with actionlistener not working.

    this is my current piece of code:

    Java Code:
    JMenuItem Home = new JMenuItem("Home");
    
    thehandler handler = new thehandler();
    
    Home.addActionListener(handler);
    
    public class thehandler implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    
    if(e.getSource()==Home){
    
    JOptionPane.showMessageDialog(null, "Home");
    
    }
    
    
    }
    }

    this doesnt work, when i put it this way it show me an error at this line:
    it gives a red line under: "Home".

    Java Code:
    if(e.getSource()==Home)

    "Home cannot be resolved to a variable".

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,646
    Rep Power
    5

    Default Re: JMenuItems with actionlistener not working.

    Your syntax is all screwed up. You need to move the declarations inside of the class. There will probably be other problems too. You should probably use a constructor to initialize your fields.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: JMenuItems with actionlistener not working.

    Quote Originally Posted by jim829 View Post
    Your syntax is all screwed up. You need to move the declarations inside of the class. There will probably be other problems too. You should probably use a constructor to initialize your fields.

    Regards,
    Jim
    it's a bit weird, because i did it just like this with my JButtons and it always worked..

    but only with these JMenuItems it gives me an error

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,646
    Rep Power
    5

    Default Re: JMenuItems with actionlistener not working.

    Normally you add menuItems to menuBars and then set the menu Bar in the hosting JFrame. You also need to organize your declarations and initializations within the class definition and a constructor appropriately. And convention dictates that class names should start with a capital letter and methods and fields start with a lower case letter.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: JMenuItems with actionlistener not working.

    Quote Originally Posted by MR bruto View Post
    it's a bit weird, because i did it just like this with my JButtons and it always worked..

    but only with these JMenuItems it gives me an error
    What error? A runtime error? A compile time error? Don't make us guess; if you want help you need to give us all the relevant information. Programming is not voodoo, nor are most of us psychic. With the little information you gave, all we can say is check the batteries for your JMenuItem objects; maybe they're dead.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: JMenuItems with actionlistener not working.

    Quote Originally Posted by JosAH View Post
    What error? A runtime error? A compile time error? Don't make us guess; if you want help you need to give us all the relevant information. Programming is not voodoo, nor are most of us psychic. With the little information you gave, all we can say is check the batteries for your JMenuItem objects; maybe they're dead.

    Jos
    kk ill give you the whole code.
    Java Code:
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemListener;
    import java.awt.image.BufferedImage;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    public class FishingCalculator2 extends JFrame {
    	
    	
    	
    	
    	private JButton b1;
    	private JButton b2;
    	
    
    	public FishingCalculator2(){
        	      super("Fishing Calculator");
        			
        	      
        	        //Font
        	      Font font = new Font("Impact", Font.PLAIN, 13);
        			//Icons
        			Icon img3 = new ImageIcon(getClass().getResource("xpicon.gif"));
        			Icon img = new ImageIcon(getClass().getResource("Fishingicon2.gif"));
        			
        			
        			//JPanel
        			JPanel panel = new JPanel();
        			panel.setLayout(new GridLayout(0, 2, 40, 30));
        			//JMenu's
        			JMenuBar bar = new JMenuBar();
        			JMenu menu = new JMenu("Other Calculators..");
        			JMenu menu1 = new JMenu("About");
        			JMenu ultimatescape = new JMenu("UltimateScape");
        			JMenuItem home = new JMenuItem("Home");
        			JMenuItem Hiscores = new JMenuItem("Hiscores");
        			JMenuItem Vote = new JMenuItem("Vote");
        			JMenuItem Store = new JMenuItem("Store");
        			JMenuItem Support = new JMenuItem("Support");
        			JMenuItem Rules = new JMenuItem("Rules");
        			JMenuItem calcs = new JMenuItem("More calculators");
        			JMenuItem Pisvlekk = new JMenuItem("Pisvlekk");
        			JMenuItem play = new JMenuItem("Play");
        			//JLabels
        			JLabel lbl1 = new JLabel("Current XP: ");
        			JLabel lbl2 = new JLabel("XP goal: ");
        			JLabel lbl3 = new JLabel("Name of fish: ");
        			JLabel lbl4 = new JLabel("Made by Pisvlekk ©");
        			lbl4.setFont(new Font("Impact", Font.PLAIN, 10));
        			lbl4.setForeground(Color.RED);
        			lbl1.setFont(font);
        			lbl2.setFont(font);
        			lbl3.setFont(font);
        			//JTextFields
        			JTextField txt1 = new JTextField(10);
        			JTextField txt2 = new JTextField(10);
        			JTextField txt3 = new JTextField(10);
        			txt1.setBackground(new Color(135,206,235));
        			txt2.setBackground(new Color(135,206,235));
        			txt3.setBackground(new Color(135,206,235));
        			//JButtons
        			b1 = new JButton("Calculate");
        			b2 = new JButton("XP Table");
        			b1.setBackground(new Color(135,206,235));
        			b1.setForeground(Color.BLACK);
        			b2.setBackground(new Color(135,206,235));
        			b2.setForeground(Color.BLACK);
        			b1.setIcon(img);
        			b2.setIcon(img3);
        			b1.setFont(font);
        			b2.setFont(font);
        			//Alles adden
        			panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        			panel.add(lbl1); 
        			panel.add(txt1);
        			panel.add(lbl2);
        			panel.add(txt2);
        			panel.add(lbl3);
        			panel.add(txt3);
        			panel.add(b2);
        			panel.add(b1);
        			panel.add(lbl4);
        			bar.add(ultimatescape);
        			bar.add(menu1);
        			bar.add(menu);
        			menu.add(calcs);
        			menu1.add(Pisvlekk);
        			ultimatescape.add(Home);
        			ultimatescape.add(Vote);
        			ultimatescape.add(Store);
        			ultimatescape.add(play);
        			ultimatescape.add(Hiscores);
        			ultimatescape.add(Rules);
        			ultimatescape.add(Support);
        			add(panel);
        			setJMenuBar(bar);
        			bar.setVisible(true);
        			panel.setBackground(new Color(255,255,102));
     
        			thehandler handler = new thehandler();
        			b1.addActionListener(handler);
        			b2.addActionListener(handler);
        		   		
        	
        }
    		
    		public class thehandler implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				Icon img2 = new ImageIcon(getClass().getResource("xptable2.gif"));
    				if(e.getSource()==b2){
    					JOptionPane.showMessageDialog(null, "", "XP Table", JOptionPane.PLAIN_MESSAGE, img2);}		
    			
    				if(e.getSource()==home){
    					JOptionPane.showMessageDialog(null, "dick");
    				
    			}
    		
    		}
    }
    }
    when you hover over "home" in the thelistener class with your mouse: "home cannot be resolved to a variable".
    that's the only problem, but for my JButtons it just works properly. but somehow for JMenuItem it won't work

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,646
    Rep Power
    5

    Default Re: JMenuItems with actionlistener not working.

    Two problems here.

    1. "Home" should be "home"
    2. You declare home within the constructor. This makes it a local variable. So the handler can't see it.

    If you plan on using any of those variables in the rest of the class, then move the declarations outside of the constructor. You can initialize them inside the constructor.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: JMenuItems with actionlistener not working.

    alright, thanks for the help mate

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: JMenuItems with actionlistener not working.

    Quote Originally Posted by MR bruto View Post
    when you hover over "home" in the thelistener class with your mouse: "home cannot be resolved to a variable".
    that's the only problem, but for my JButtons it just works properly. but somehow for JMenuItem it won't work
    That's because 'home' is a variable local to the constructor while those buttons are class instance variables; it certainly would help you a lot if you'd read (and study) some tutorials for the basics of the Java language.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. JMenuItems with actionlistener of itemlistener?
    By MR bruto in forum New To Java
    Replies: 3
    Last Post: 06-06-2013, 03:13 PM
  2. ActionListener not working
    By aianta in forum New To Java
    Replies: 4
    Last Post: 09-17-2012, 06:15 AM
  3. Why is my ActionListener not working
    By isaacske in forum AWT / Swing
    Replies: 2
    Last Post: 09-07-2012, 10:35 PM
  4. actionlistener no working,, help
    By SaraDiab in forum Advanced Java
    Replies: 2
    Last Post: 12-27-2011, 05:22 AM
  5. ActionListener NOT working
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 03:40 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
  •