I'm getting errors, and I'm not sure why. Any help would be appreciated. I'm working on this program and when I put the ActionListener for the comboBox it blows up. I chose a topic from the comboBox that will pick a file to be read. When I comment out lines 127 to 130 it runs.

Any help is appreciated.

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Test extends JFrame {

	public static final int WIDTH = 640;
	public static final int HEIGHT = 500;
	JPanel upper;
	JPanel middle;
	JPanel lower;
	JTextArea questionArea;
	JTextArea choicesArea[];
	JTextArea choiceFields[];
	JTextArea outputArea;
	JButton choiceButtons[];
	JButton nextButton;
	final String[] typeOfTestPath = { "java.txt", "algebra.txt" };
	final String[] typeOfTestList = { "Java", "Albegra" };
	JComboBox typeOfTestBox;
	final int NUM_OF_BUTTONS = 4;
	String buttonLabels[] = { "A", "B", "C", "D" };
	int typeOfTest;
	// Multiple choice variables
	int numCurrentQuestion;
	int numQuestions;
	int score;
	int maxScore;
	boolean questionAnswered;
	ShuffleVector questionList;
	String testChoice;

	// parser variables
	Question q;
	Answer a;
	int value;
	boolean shuffleAnswers;
	int mode;
	final int GLOBAL_MODE = 0;
	final int QUESTION_MODE = 1;
	final int ANSWER_MODE = 2;

	Vector<String> fileDescriptions;
	Vector<String> fileNames;

	public Test() {

		setSize(WIDTH, HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		int i;

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(upper = new JPanel(), BorderLayout.NORTH);
		getContentPane().add(middle = new JPanel(), BorderLayout.CENTER);
		upper.setLayout(new GridBagLayout());
		upper.setLayout(new GridBagLayout());
		upper.setBackground(Color.green);
		middle.setBackground(Color.green);
		// uppper third of JFrame

		// getFiles();
		addComponent(upper, new JLabel("Select category: "), 0, 0);
		addComponent(upper, typeOfTestBox = new JComboBox(), 0, 1);
		
		addComponent(upper, questionArea = new JTextArea(4, 50), 1, 0, 3, 3);
		questionArea.setLineWrap(true);
		questionArea.setWrapStyleWord(true);
		questionArea.setEditable(false);
		for (i = 0; i < typeOfTestList.length; i++) {
			typeOfTestBox.addItem(typeOfTestList[i]);
		}
		// middle third of JFrame

		choiceButtons = new JButton[NUM_OF_BUTTONS];
		choiceFields = new JTextArea[NUM_OF_BUTTONS];

		for (i = 0; i < NUM_OF_BUTTONS; i++) {
			addComponent(middle,
					choiceButtons[i] = new JButton(buttonLabels[i]), i, 0);
			addComponent(middle, choiceFields[i] = new JTextArea(2, 50), i, 1,
					2, 1);

			choiceFields[i].setLineWrap(true);
			choiceFields[i].setWrapStyleWord(true);
			choiceFields[i].setEditable(false);
		}

		// lower third of JFrame

		getContentPane().add(outputArea = new JTextArea(8, 40),
				BorderLayout.SOUTH);
		getContentPane().add(lower = new JPanel(), BorderLayout.SOUTH);
		addComponent(lower, nextButton = new JButton("Next Question >>"), 0, 2);
		outputArea.setBackground(Color.green);
		outputArea.setLineWrap(true);
		outputArea.setWrapStyleWord(true);
		// questionArea.setEditable(false);

		// get the questions
		//getQuestions("");
		//displayQuestion();
	}

	private void addComponent(JPanel p, Component c, int row, int column,
			int width, int height) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = column;
		gbc.gridy = row;
		gbc.gridheight = height;
		gbc.gridwidth = width;
		p.add(c, gbc);
	}

	private void addComponent(JPanel p, Component c, int row, int column) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = column;
		gbc.gridy = row;
		p.add(c, gbc);

		typeOfTestBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int j = typeOfTestBox.getSelectedIndex();
				typeOfTest = j;
				}});
				/*
				if (typeOfTestPath[j].length() == 0 && j > 0) {
					return;
				}
				//getQuestions(typeOfTestPath[typeOfTest]);
				//displayQuestion();
			}
		});*/
	}


	public static void main(String[] args) {

		Test gui = new Test();
		gui.setVisible(true);
	}

}

Recommended Answers

All 7 Replies

The error message (Exception) tells you exactly what kind of error it is, and exactly which line it happens on. Without that info its very hard to debug.
Please post the full text of the error message

Sorry, I forgot to post the error its

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at Test.main(Test.java:150)
and line 150 => 143 here

It appears to be in my actionListener, when I comment it out, I get my gui. I have tried changing my class to public class Test extends JFrame implements ActionListener etc. with the same results

Unresolved compilation problem

So what was the compile error (the compiler will have given you an error message)?
(maybe something to do with the stray brackets/parens on line 139?)

I'm using eclipse and the only errors are about the bracket on 126 when I have one, it wants it to be at 131, or 139 depending on what's commented out.

Java error messages are very precise and contain a lot of info. By not posting the full exact error message you make this a lot harder than it has to be.
Anyway - you obviously have some kind of unmatched bracket problem, so you need to fix that. Its completely pointless to try to run or debug code that didn't compile properly.

move 
typeOfTestBox.addActionListener(new ActionListener() {
from
private void addComponent(JPanel p, Component c, int row, int column) {

and place that bellow typeOfTestBox declatations

at line 71.

maybe for you attack would be bettet implement

typeOfTestBox.addItemListener(new ItemListener() {

http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html

Thanks, I moved everything like you suggested, and that is getting rid of my problems so far. I'll try moving my other ActionListeners back in the same fashion before trying to create questions and reading from files this time.

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.