Results 1 to 12 of 12
  1. #1
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default What does this java error mean?

    Java Code:
    A3JPanel.java:9: A3JPanel is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener
    public class A3JPanel extends JPanel implements KeyListener {

    Relevant code
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class A3JPanel extends JPanel implements KeyListener {
    	private boolean showTitleScreen;
    
     	public A3JPanel() {
    		setBackground(Color.white);
    		showTitleScreen = true;
    		addKeyListener(this);
    	}
    	public void keyPressed(KeyEvent e) {
    		e.getKeyChar();
    		showTitleScreen = false;	
    	}

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    When you define a class as implementing an interface, you need to implement all the methods of that interface. You have implemented keyPressed, but you also need to implement keyReleased to complete the requirements of implementing KeyListener.
    Get in the habit of using standard Java naming conventions!

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    ...and also keyTyped.

    If your class did not already extend JFrame, you could extend KeyAdapter, which implements all the methods of KeyListener. Then you only have to override the methods you care about. There are Adapter classes for (I think) every Listener interface in the standard library.
    Get in the habit of using standard Java naming conventions!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kjkrum View Post
    ...and also keyTyped.

    If your class did not already extend JFrame, you could extend KeyAdapter, which implements all the methods of KeyListener.
    Nothing a small inner class (that extends the KeyAdapter class) can't solve ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    One problem with using adapters is that if you misspell the name of the method you want to override, the compiler adds it as a new method. Be sure to use the @Override statement to test that you have correctly spelled the method name.
    This is from experience.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    One problem with using adapters is that if you misspell the name of the method you want to override, the compiler adds it as a new method. Be sure to use the @Override statement to test that you have correctly spelled the method name.
    This is from experience.
    That's why I like Eclipse so much: it displays a nice little green icon in the left margin if my method overrides a method in the parent class. A white triangle is shown if the method implements a method mentioned in an interface. Before I used Eclipse I've been bitten by my own typos often, especially my actionPreformed( ... ) methods are famous ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Ah I see, cheers for your help guys. I was copying off another code and realised these two methods were hiding at the bottom :)

    Java Code:
        public void keyReleased(KeyEvent e) {} 
        public void keyTyped(KeyEvent e) {}

  8. #8
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Anyone know how I can get a "Press any key to continue" style thing to work? ,
    Java Code:
    e.getKeyChar();
    didn't seem to work.

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Unless anyone else can offer light on this one, google has not returned anything useful. Reading a couple of other forums with the same question most of them have been resolved to "Press Enter to continue." Instead of any key.

    You could use that if all else fails, which should just be a simple key press.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Show us your code so we can see the problem, and error message if has.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    how I can get a "Press any key to continue" style thing to work
    Normally that message is when the program is waiting for user input.
    How is your program waiting? Does it display a modal dialog that suspends execution until there is user action?

    Can you explain the state of execution of your code?

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    The thread continues here:
    Press enter to continue (Jpanel)
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Replies: 0
    Last Post: 01-21-2011, 12:43 PM
  2. Replies: 5
    Last Post: 08-13-2010, 10:04 AM
  3. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  4. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  5. Replies: 1
    Last Post: 10-26-2009, 04:38 PM

Posting Permissions

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