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: Learning ActionListeners, help please.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Learning ActionListeners, help please.

    Alright, so I'm trying to learn how to make GUI's. I've managed to construct a frame, panel, menubars, items etc. Now I'm trying to get it so something happens when I click in the menu. I've followed a few different tutorials online, and also read through a textbook I bought. However, no matter what I try, I keep getting this error.

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	No enclosing instance of type RiskMain is accessible. Must qualify the allocation with an enclosing instance of type RiskMain (e.g. x.new A() where x is an instance of RiskMain).
     
    	at RiskMain.createWindow(RiskMain.java:43)
    	at RiskMain.main(RiskMain.java:34)


    the code is

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
     
     
     
    public class RiskMain {
     
    	private static JFrame mainFrame = new JFrame("Risk Processor");
     
    	private static JMenuBar menuBar = new JMenuBar();
     
    	private static JMenu fileMenu = new JMenu("File");
    	private static JMenu gameMenu = new JMenu("Game");
    	private static JMenu playerMenu = new JMenu("Player");
     
     
     
    	private static JMenuItem newGame = new JMenuItem("New");
    	private static JMenuItem openGame = new JMenuItem("Open");
    	private static JMenuItem saveGame = new JMenuItem("Save");
    	private static JMenuItem quitGame = new JMenuItem("Quit");
    	private static JMenuItem newDay = new JMenuItem("New Day");
    	private static JMenuItem genText = new JMenuItem("Generate Text");
    	private static JMenuItem addAttack = new JMenuItem("Add Attack");
    	private static JMenuItem purchItem = new JMenuItem("Purchase Item");
     
        public static void main(String []args) {
     
        	createWindow();
     
        }
        public static void createWindow() {
        	menuBar.add(fileMenu);
        	menuBar.add(gameMenu);
        	menuBar.add(playerMenu);
     
        	fileMenu.add(newGame);
        		newGame.addActionListener(new MenuItemListener()); 
        	fileMenu.add(openGame);
        	fileMenu.add(saveGame);
        	fileMenu.add(quitGame);
     
        	gameMenu.add(newDay);
        	gameMenu.add(genText);
     
        	playerMenu.add(addAttack);
        	playerMenu.add(purchItem);
     
        	mainFrame.setJMenuBar(menuBar);
     
        	mainFrame.setVisible(true);
        	mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	mainFrame.setSize(1200,800);
     
        }
     
        private class MenuItemListener implements ActionListener {
     
        	public void actionPerformed(ActionEvent e) {
        		JOptionPane.showMessageDialog(null, "click");
     
        	}
        }  
    }

    I appreciate any help and incite that can be offered.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Learning ActionListeners, help please.

    A couple of solutions:
    1)Move the code out of the static method into an instance method in the RiskMain class.
    2)Make the MenuItemListener class static so it exists without needing an instance of the RiskMain class.
    3)Use the technique described in the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning ActionListeners, help please.

    Move which code out?

    Also, I don't understand what the error message is telling me to do. What does it mean by qualifying the allocation with an enclosing instance?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Learning ActionListeners, help please.

    Make the createWindow() method an instance method NOT static.

    The MenuItemListener class belongs to the RiskMain class. You need to create an instance of the RiskMain class to use to create an instance of the MenuItemListener class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning ActionListeners, help please.

    Quote Originally Posted by Norm View Post
    Make the createWindow() method an instance method NOT static.

    The MenuItemListener class belongs to the RiskMain class. You need to create an instance of the RiskMain class to use to create an instance of the MenuItemListener class.
    Ah, okay, that makes sense. I have to go for now, so I can't give it a try just yet, but I'll give it a go later today and post back my success/failure. Thanks for your help so for

Similar Threads

  1. adding ActionListeners dynamically
    By Hikaros in forum Java Theory & Questions
    Replies: 18
    Last Post: November 5th, 2013, 05:10 PM
  2. Connecting Two Different ActionListeners
    By steme in forum Java Theory & Questions
    Replies: 1
    Last Post: April 26th, 2013, 08:35 AM
  3. How to use multiple actionlisteners
    By pottsiex5 in forum AWT / Java Swing
    Replies: 9
    Last Post: November 19th, 2011, 09:37 AM
  4. [SOLVED] Quick question regarding ActionListeners
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: October 4th, 2011, 12:02 PM
  5. [SOLVED] NullPointerException on ActionListeners
    By cherryduck in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 7th, 2010, 04:17 PM