Results 1 to 4 of 4
  1. #1
    ryanonnfire54 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Problem implementing ActionListener??

    When making the constructor for an ActionListener, I am receiving the error:

    Java Code:
    No enclosing instance of type Voting is accessible. 
    Must qualify the allocation with an enclosing instance of type Voting 
    (e.g. x.new A() where x is an instance of Voting).
    I'm not sure what this is even concerned with (other than the fact it is a problem with the action listener).


    The following code is just the code I have started and is in no way finished :(

    Voting class:


    Java Code:
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JWindow;
    
    
    
    
    public class Voting{
    	
    	
    	public static void main(String[] args){
    		
    		MyListener theListener = new MyListener(); //eclipse error mark under this constructor
    		
    		try {
    			
    			Scanner sc = new Scanner(new File("ballots.txt"));
    			Ballot[] ballots = new Ballot[Integer.parseInt(sc.nextLine())];
    			System.out.println(ballots.length);
    			createGUI(ballots, sc, theListener);
    			getVoters();
    			
    		} catch (FileNotFoundException e) {
    			JOptionPane.showMessageDialog(null, "ERROR: FILE DOES NOT EXIST.\nLaunch again with correct file.", "ERROR", JOptionPane.ERROR_MESSAGE);
    			System.exit(0);
    			e.printStackTrace();
    		}
    		
    		
    	}
    
    	private static void getVoters() {
    		// TODO Auto-generated method stub
    		
    	}
    
    	private static void createGUI(Ballot[] ballots, Scanner sc, MyListener theListener) {
    		JFrame theWindow = new JFrame("Vote here");
    		theWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    
    		//makes the pane
    		Container thePane = theWindow.getContentPane();
    		thePane.setLayout(new GridLayout(1, ballots.length + 2));
    		
    		for(int i = 0; i < ballots.length; i++){
    			//System.out.println("Accessed for loop");
    			ballots[i] = new Ballot();
    			ballots[i].getBallotInformation(sc);
    			ballots[i].setPanel(theListener); //eclipse marks the error under the "setPanel" method here as well
    			thePane.add(ballots[i]);
    		}
    		sc.close();
    		
    	}
    	
    class MyListener implements ActionListener{
    	
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
    				
    		}
    	}
    }

    Ballot class:


    Java Code:
    import java.awt.GridLayout;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    @SuppressWarnings("serial")
    public class Ballot extends JPanel{
    	
    	private String title;
    	private ArrayList<String> answers;
    	private JLabel panelTitle;
    	private ArrayList<JButton> buttonAnswers;
    	
    	public void getBallotInformation(Scanner sc){
    		System.out.println("Accessed method");
    		StringTokenizer ballotParse = new StringTokenizer(sc.nextLine(), ":,");
    		title = ballotParse.nextToken();
    		//System.out.println(title);
    		answers = new ArrayList<String>();
    		while(ballotParse.hasMoreTokens()){
    			answers.add(ballotParse.nextToken());
    			//System.out.println(answers);
    		}
    	}
    
    	public void setPanel(MyListener theListener) { //error mark in parameters list here
    		buttonAnswers = new ArrayList<JButton>();
    		setLayout(new GridLayout(answers.size(), 1));
    		setSize(50,50);
    		panelTitle = new JLabel(title);
    		add(panelTitle);
    		for(int i = 0; i < answers.size(); i++){
    			buttonAnswers.add(new JButton(answers.get(i)));
    			buttonAnswers.get(i).addActionListener(theListener);
    			add(buttonAnswers.get(i));
    		}
    		setEnabled(false);
    	}
    	
    	
    
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Ok you have two options:

    - put the MyListener class in their own new file!
    - change the MyListener class to a public static class !

    if you choose the second option, you have to change the setPanel method to something like public void setPanel(Voting.MyListener theListener)

  3. #3
    ryanonnfire54 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Excellent, worked perfectly. Thank you very much!

    Just a follow up question though, how will making the MyListener class static change the way it works?

    Thanks

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,381
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ryanonnfire54 View Post
    Just a follow up question though, how will making the MyListener class static change the way it works?
    Objects of an inner class are like planets orbiting a star, an object of the outer class; when there's no star there can't be planets. A nested static class is just a scoping rule: the static class is defined lexically inside the outer class and no objects are involved in this scenario. It only depends on the access rules whether or not the outside world can reach such a class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Problem Actionlistener
    By Questionmark in forum New To Java
    Replies: 18
    Last Post: 08-07-2010, 12:29 PM
  2. ActionListener run automatically problem
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:42 PM
  3. Replies: 8
    Last Post: 03-09-2009, 11:32 AM
  4. ActionListener Applet problem
    By xander5511 in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:42 AM

Posting Permissions

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