Results 1 to 4 of 4
  1. #1
    xael is offline Member
    Join Date
    Sep 2010
    Rep Power

    Default Can't implement listeners

    I'm trying to pick up Java, and have successfully built a GUI. I'm now trying to get event listening to work; which is where I'm having trouble. After declaring the class, I type in "implements ActionListener", it (sometimes) won't give me an error, but if I implement any other listener, such as ItemListener or KeyListener, then I get an error saying that the class Main is not abstract and doesn't override something. Here's the start of my class

    package guibythebook;

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

    public class Main extends JFrame implements ItemListener{

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Bali, Indonesia
    Rep Power



    The error message already said what you have to fix with your code. It said that you have to create the method of the interface that your class implementing. You have to follow the contract of the interface.

    For example if you want to implement the ItemListener interface then your class should implements the itemStateChanged(ItemEvent e) method defined by the ItemListener interface. Or else your class should be an abstract class.

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    well sure, each listener interface has its own kind of method you need to have in your Main class, for example, in this case ItemListener wants you to create a
    public void itemStateChanged(ItemEvent e) {
    // TODO:

    I find the Java doc very helpful for the specific interface you want to implement to figure out what methods you need to add to your class. Depending on your IDE, there might be a way to "add missing methods" that are needed to implement the interfaces.

    Edit: D'oh.. Too slow in my replying.

  4. #4
    xael is offline Member
    Join Date
    Sep 2010
    Rep Power

    Default Solved

    thanks! Didn't realize I needed a corresponding method later on, but that fixed it

Similar Threads

  1. Help with Listeners
    By Psyclone in forum AWT / Swing
    Replies: 8
    Last Post: 02-09-2010, 08:21 PM
  2. jTextField Listeners
    By user6123456 in forum AWT / Swing
    Replies: 6
    Last Post: 10-15-2009, 06:58 PM
  3. Breaking for-loops with listeners?
    By CBarry in forum New To Java
    Replies: 3
    Last Post: 04-22-2009, 04:38 AM
  4. Help Updating Tree via Listeners
    By Seiki in forum SWT / JFace
    Replies: 1
    Last Post: 03-27-2009, 11:46 AM
  5. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 11:41 AM

Tags for this Thread

Posting Permissions

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