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

    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
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Hi,

    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
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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
    <pre>
    public void itemStateChanged(ItemEvent e) {
    // TODO:
    }
    </pre>
    method.

    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
    Posts
    31
    Rep Power
    0

    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, 07:21 PM
  2. jTextField Listeners
    By user6123456 in forum AWT / Swing
    Replies: 6
    Last Post: 10-15-2009, 05:58 PM
  3. Breaking for-loops with listeners?
    By CBarry in forum New To Java
    Replies: 3
    Last Post: 04-22-2009, 03:38 AM
  4. Help Updating Tree via Listeners
    By Seiki in forum SWT / JFace
    Replies: 1
    Last Post: 03-27-2009, 10:46 AM
  5. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 10: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
  •