Problem implementing ActionListener??
When making the constructor for an ActionListener, I am receiving the error:
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:
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:
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);
}
}