Results 1 to 7 of 7
  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Do you have to overwrite all abstract methods?

    I think the title is right ( That might not even be what im doing), im learning from thenewboston (who isnt...).

    Java Code:
    	private class keyHandler implements KeyListener {
    
    		public void keyPressed(KeyEvent key) {
    		}
    
    		public void keyReleased(KeyEvent key) {
    
    		}
    
    		public void keyTyped(KeyEvent key) {
    
    		}
    
    	}
    ^This is my handling class for key pressing.
    The thing is I don't want to handle pressed and typed- just released!
    Is there any way ( other than leaving them blank) to do this?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default Re: Do you have to overwrite all abstract methods?

    Instead of implementing the interface you can extend KeyAdapter. Then just override the one you want.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Do you have to overwrite all abstract methods?

    Awesome, but is there any other way .. if i stumble across this again - but with a ..class?.. that doesn't have an adapter?

    EDIT: Problem again
    Java Code:
    package xyexs.name.src;
    
    import java.awt.BorderLayout;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Frame extends JFrame {
    
    	private JPanel panel;
    
    	public Frame() {
    		super(Reference.PROGRAM_NAME + " " + Reference.VERSION);
    		setLayout(new BorderLayout());
    		setSize(Reference.WINDOW_X, Reference.WINDOW_Y);
    		setResizable(false);
    
    		keyHandler handlerObject = new keyHandler();
    
    		panel = new JPanel();
    		panel.setBackground(Reference.BACKGROUND_COLOR);
    		panel.addKeyListener(handlerObject);
    		add(panel, BorderLayout.CENTER);
    	}
    
    	private class keyHandler extends KeyAdapter{
    
    		public void keyReleased(KeyEvent key) {
    			if (key.getKeyCode() == 38){
    				System.out.println("ITS FEHKING WORKING");
    			}
    		}
    	}
    }
    This adds a frame but does nothing on key press- help?
    Last edited by Xyexs; 08-21-2013 at 08:17 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default Re: Do you have to overwrite all abstract methods?

    Most of the common interfaces that support events and have more than one method have adapter classes. The only classes which have methods that you must implement are those declared abstract in abstract classes. Note: all interfaces and their methods are inherently abstract.

    Other classes have dummy methods which you may or may not choose to override depending on the class and what you want to achieve.

    Regards,
    Jim
    Last edited by jim829; 08-21-2013 at 08:38 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Do you have to overwrite all abstract methods?

    Awesome, see edit on last post!

    Edit: I just realised how much i say "Awesome!"...
    Last edited by Xyexs; 08-21-2013 at 08:29 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default Re: Do you have to overwrite all abstract methods?

    panel.setFocusable(true);

    Also, in general I would recommend you reverse the roles of the panel and the frame. Extend JPanel but create an instance of the JFrame to which the panel will be added.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Do you have to overwrite all abstract methods?

    Quote Originally Posted by Xyexs View Post
    Java Code:
    	private class keyHandler implements KeyListener {
    By convention, Java class names start with an uppercase letter. See Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Overloading abstract methods??!?
    By tfitz666 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 09:45 AM
  4. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:36 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
  •