views:

49

answers:

2

JFrameWithPanel is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener public class JFrameWithPanel extends JFrame implements ActionListener

I Don't get this code. Book and Java site tells me this is the syntax for the method, but again this error shows up constantly.

import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.lang.Integer.*;
import java.util.*;
import java.util.Random;
import java.io.*;
import java.text.*;
import java.text.DecimalFormat.*;

public class JFrameWithPanel extends JFrame implements ActionListener
{
 JButton button = new JButton("Exit");

 public JFrameWithPanel()
 {
  super("JFrame with Panel");

  JComboBox packageChoice = new JComboBox();
  packageChoice.addItem("A+ Certification");
  packageChoice.addItem("Network+ Certification ");
  packageChoice.addItem("Security+ Certifictation");
  packageChoice.addItem("CIT Full Test Package");

  packageChoice.addActionListener(this);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JPanel pane = new JPanel();
  pane.add(button);
  pane.add(packageChoice);
  setContentPane(pane);
  setSize(200,100);
  setVisible(true);

 }
}

then later

public class CreateJFrameWithPanel
{
 public static void main(String[] args)
 {
  JFrameWithPanel panel = new JFrameWithPanel();
 }
}
+2  A: 

The class implements the ActionListener interface. This means that it must implement a method:

public void actionPerformed(ActionEvent)

However, the class definition you've posted does not include this method, hence why you are seeing a compilation error. To fix the code, try adding the following method:

public void actionPerformed(ActionEvent evt) {
  Object obj = packageChoice.getSelectedItem();
  JOptionPane.showMessageDialog(this, "You selected: " + obj);
}
Adamski
I get an error now with the getSelectionItem()
Nick Gibson
It should have read "getSelectedItem()" (not corrected). You should try and provide more information on errors; otherwise it becomes difficult / impossible to diagnose the problem. Also, with this type of compilation error the first thing you should do is check the JDK API to see whether the method exists on the class (JComboBox). Hope that helps.
Adamski
Good answer. Here's a couple additional hints: If you are using Eclipse, you will see an error "X" on the left side. If you click it then it can correct the problem for you by adding the framework for the method you are trying to override; also if you are using Extends instead of Implements, be sure to use @override in front of your method. @override will warn you if the method signature is incorrect (this is not really necessary with implements since you will already get the error that you got, but with extends you wouldn't have gotten any errors, your listener would simply never fire)
Bill K
+1  A: 

implements ActionListener means that your class must defined the methods that are defined in the ActionListener interface. It has one method:

void actionPerformed(java.awt.event.ActionEvent);

So you must either have this method. You need it, because your button needs an action listener.

In that method you define what happens when the button is clicked.

Bozho