Results 1 to 5 of 5

Thread: Event Listeners{resolved}

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Event Listeners{resolved}

    I copied this program right out of the book and I keep getting an error about something not being an abstract method and not overriding one.

    Here is the code for the program:

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

    class KeyView extends JFrame implements KeyListener {

    JTextField keyText = new JTextField(80);
    JLabel keyLabel = new JLabel("Press any key in the text field");

    KeyView() {
    super("KeyView");
    setSize(350,100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    keyText.addKeyListener(this);
    Container pane = getContentPane();
    BorderLayout bord = new BorderLayout();
    pane.add(keyText, BorderLayout.NORTH);
    pane.add(keyLabel, BorderLayout.CENTER);
    setContentPane(pane);
    setVisible(true);

    }

    public void KeyTyped(KeyEvent input) {
    char key = input.getKeyChar();
    keyLabel.setText("You pressed " + key);
    }

    public void keyPressed(KeyEvent txt) {
    //do nothing
    }
    public void keyReleased(KeyEvent txt) {
    //do nothing
    }

    public static void main(String[] arguments) {
    KeyView frame = new KeyView();
    }
    }

    This is the error message I get

    KeyView is not abstract and does not override abstract method keyTyped(java.awt.event.KeyEvent) in java.awt.event.KeyListener
    class KeyView extends JFrame implements KeyListener {




    Somebody pleeeeeeeaaaase help me...
    Last edited by Dilenger4; Jun 6th, 2004 at 09:54 AM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Just extend the KeyAdapter class so you can forget about the method stubs.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I think what you need to do is provide implementation for the method stubs that are contained within the KeyListener interface by adding ;'s. You are better off just extending the KeyAdapter class which implements the KeyListener class.
    Code:
    public void KeyTyped(KeyEvent input) {
    char key = input.getKeyChar();
    keyLabel.setText("You pressed " + key);
    }
    
    public void keyPressed(KeyEvent txt) {;}
    
    public void keyReleased(KeyEvent txt) {;}

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Sorry i was wrong. Stubs need no ;'s inside the {}'s but i did find out what is wrong with your code. You have to define your method as
    Code:
     public void keyTyped(KeyEvent input) {}

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Thank you so much.
    I don't know how many times I have capitilized something wrong and never been able to find the error.

    Again thank you.

Posting Permissions

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



Click Here to Expand Forum to Full Width