My program won't let me use the "implements" keyword?

I wrote this program to tinker with event listeners and such.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame {
	public Test() {
		super("Test");
		setSize(300, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JTextField searchBar = new JTextField(20);
		searchBar.addActionListener(
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						// do something
					}
				});
		JButton search = new JButton("Search");
		
		FlowLayout flo = new FlowLayout();
		setLayout(flo);
		
		add(searchBar);
		add(search);
		
		setVisible(true);
	}
	
	public static void main(String[] args) {
		Test test = new Test();
	}
}

If I leave the program like that it’s fine. But if I try to do “implements ActionListener” at the top, it gives me a warning. I’m using Eclipse, and it says, “The type Test must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)” Why is it doing this? It won’t even let me do “searchBar.addActionListener(this)” and then “public void actionPerformed(ActionEvent evt) {}” How come? Thanks for the help.

That’s some core java issues. Try reading official tutorial http://java.sun.com/docs/books/tutorial/java/IandI/index.html this might help.

In short: if your class is implementing some interface it must do one of the following:

either implement all of the methods declared in the interface
or be declared as abstract.

Your class is not abstract so it must implement actionPerformed.

But it does implement actionPerformed. Doesn’t it? :-\

Try this;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame implements ActionListener {

	JTextField searchBar;
	JButton search;

	public Test() {
		super("Test");
		setSize(300, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		searchBar = new JTextField(20);
		search = new JButton("Search");

		search.addActionListener(this);

		FlowLayout flo = new FlowLayout();
		setLayout(flo);

		add(searchBar);
		add(search);

		setVisible(true);
	}

	public void actionPerformed(ActionEvent e)
	{
		// if search button clicked
		if (e.getSource()==search)
		{
			// get search string
			String searchString=searchBar.getText();
			// do search...
		}
	}

	public static void main(String[] args) {
		Test test = new Test();
	}
}

Thanks, I got it now. But now I’m having trouble with my program skipping my if statement. I made my search button compare the text in the JLabel to the text in the JTextField, and it doesn’t work. (My program will eventually be used to help build my Spanish vocabulary. The JLabel displays a Spanish word, and you have to type the English in the text box. If it’s right, something will pop up and say “Correct!” I currently have it so that the JTextField says correct or incorrect, just because it’s the concept that matters right now. I will also eventually make an array that has English in it, because right now I’m only comparing Spanish to Spanish. I’d like to just get the program working like normal before I get into adding words to the array.) Anyway, here’s my program…

import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame implements ActionListener {
	Random rand = new Random();
	
	ArrayList<String> dictionary = new ArrayList<String>();
	
	JLabel label1 = new JLabel();
	JTextField searchBar = new JTextField(20);
	JButton correct = new JButton("Correct?");
	
	public Test() {
		super("Test");
		setSize(300, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		correct.addActionListener(this);
		
		FlowLayout flo = new FlowLayout();
		setLayout(flo);
		
		add(label1);
		add(searchBar);
		add(correct);
		
		dictionary.add("la casa");
		
		setVisible(true);
	}
	
        // check to see whether the Spanish word in the array matches the typed word.

	public void actionPerformed(ActionEvent e)
	{

		if (searchBar.getText() == label1.getText()) {
			searchBar.setText("Correct!");
		}
		
		else {
			searchBar.setText("Incorrect!");
		}
		
	}
	
	public static void main(String[] args) {
		Test test = new Test();
	}
}

Right now it judges everything as incorrect. What am I doing wrong? (Oh one more question. How come I can’t add anything to my dictionary ArrayList right after I declare it? If I try to do "dictionary.add(“example”) I get an error. The only place I can add things to it is within my constructor method. Is this a scope issue?) Thanks.

“searchBar.getText() == label1.getText()” should be “searchBar.getText().equals(label1.getText())”.

When you use ==, it compares the addresses the objects are at, not the objects themselves. If you have:

String a = "The";
String b = "The";

a == b is false, but a.equals(b) is true. This is because a and b are at different memory addresses. You have to use the equals method to actually compare the text.

An example where a == b would be true:

String a = "The";
String b = a;

In that example, a and b are the same memory address. Each Object variable really stores an address in memory where your data is. Primitive variables store the data itself.

For primitives (e.g. int, boolean, byte, etc.), you have to use ==, and it works just fine. For objects, you normally want to use the equals method. The method Object.equals actually is the same as ==, but it is overridden by most subclasses.

Actually, in that instance a==b IS true - but for an entirely different reason…

Let’s say for a beginners sake it is NOT true (even if it is ;))