Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Applet key event not recognized

  1. #1
    gord c is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Applet key event not recognized

    I have written an applet with mouse and keyboard event handlers. Some text is wriiten to the screen via the Paint() method and key strokes are supposed to be written to the screen (via a message in Paint(). Keystrokes are also written to the status bar via showStatus().

    The problem is that keystrokes are not recognized UNTIL the mouse is clicked on the applet. After that first click, the key event works as expected. It's like there is no focus on the applet to start with?

    gord c

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Applet key event not recognized

    Make sure the component with the key listener can get the focus and has the focus. The Component class has some methods that will do that. Look at the API doc for Component and Find "focus" to find the methods
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gord c is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Applet key event not recognized

    Norm, There are no (user) components in this simple applet so I can't program a requestFocus(). I tried this.requestFocus() in the Init method but this is no good. Only a physical mouse click on the applet activates the KeyListener. Perhaps it is possible to program a mouse click: that might work.
    I see that several people have had difficulty with key events (not responding). Another solution might be to use a container such as a Swing Panel and then requestFocus on that. Any comments?
    gord c

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Applet key event not recognized

    There are no (user) components in this simple applet
    The applet class extends Component.

    What other methods from the Component class did you try? There are several that are related to focus.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gord c is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Applet key event not recognized

    Quote Originally Posted by Norm View Post
    The applet class extends Component.

    What other methods from the Component class did you try? There are several that are related to focus.
    What Component? That is the question. All I have is the Applet screen which is not a Component, right? Yes, there is setFocus(), requestFocus(FocusInWindow) but to what do I apply these to.

    gord c

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Applet key event not recognized

    What Component
    The applet class by extending the Component class is a Component. See the API doc for the applet class.
    to what do I apply these to
    You can call any of the Component class's methods from within a method in the applet class.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    gord c is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Applet key event not recognized

    I've tried looking for the right construction in the API but cannot find a component of the Applet class to work with.
    Here is the bare-bones program that has the bug discussed in my posts.
    Java Code:
    /*
     * KeyTest.java
     *
     * KeyTest Applet application
     *<applet code="Main" width=300 height=100>
     *</applet>
     * Created on 02-10-2012 03:40 PM
     */
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    /* Chars typed should appear in the Status bar. They do ONLY after
        the mouse is clicked on the  Viewer Window */
    public class Main extends Applet implements KeyListener {
    	char ch; 
    	String msg="";
    	public void init() {
    	   addKeyListener(this);
    	}
    	public void keyPressed(KeyEvent k) {
    	}
    	public void keyReleased(KeyEvent k) {
    	}
    	public void keyTyped(KeyEvent k){
    	  ch=k.getKeyChar();
    	  msg+=ch;
    	  showStatus(msg);
    	}
    	public void paint(Graphics g) {		
    		g.drawString("Welcome to Java!!", 50, 60 );		
    	}
    }
    If anyone can make this work properly, I would be very pleased.
    gord c
    Last edited by Norm; 10-04-2012 at 10:45 PM. Reason: added code tags

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Applet key event not recognized

    What focus related methods from the Component class did you try?

    Please edit your post and add code tags to the code. See: BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    gord c is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Applet key event not recognized

    Please don't beat about the bush. You suggest what object.method. It's the object that has me baffled!
    gord c

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Applet key event not recognized

    It's the object that has me baffled!
    Use the this reference in the applet. What reference are you using with the following statement that calls the addKeyListener() method?
    Java Code:
    addKeyListener(this);
    What class is that method defined in?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    gord c is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Applet key event not recognized

    I have added the following code. It compiles but does not solve the keystroke problem.

    public class Main extends Applet implements KeyListener , FocusListener{
    char ch;
    String msg="";
    public void init() {
    addKeyListener(this);
    addFocusListener(this);
    this.requestFocus();
    }
    public void focusLost(FocusEvent f){
    }
    public void focusGained(FocusEvent f){
    } ....... rest of code
    I need a specific answer to this, no more vague suggestions please.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Applet key event not recognized

    gord c, Norm has given you a link to the FAQ on how to use the code tags. If you persist in posting unformatted code, this thread will be locked and you may be banned for a period.

    Kindly edit your posts and add the code tags.

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

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Applet key event not recognized

    Quote Originally Posted by gord c View Post
    I need a specific answer to this, no more vague suggestions please.
    You don't seem to understand how a forum works. We can help you to learn enough to solve your own problems, but we won't do your coding for you.

    Recommended reading: How to ask questions the smart way

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

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Applet key event not recognized

    I need a specific answer to this,
    List the methods in the Component class that deal with setting and requesting focus.
    The posted code shows just one. There are more.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Applet key event not recognized

    Quote Originally Posted by gord c View Post
    I need a specific answer to this, no more vague suggestions please.
    I was raised to appreciate help given and to not order volunteer helpers about. Perhaps things are different where you're from?
    DarrylBurke likes this.

  16. #16
    gord c is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Applet key event not recognized

    I apologize for not using the CODE tags, I don't know how to edit my previous listings. The original problem appears to be non-trivial. The failure of the applet window to gain focus (to accept keystrokes) automatically seems to be a topic that is seen in Google and other searches but no solution is available. I'm using IE8 with XP.
    Thanks
    gord c

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Applet key event not recognized

    Have you tried the AppletViewer or another browser to see if IE8 is the problem?

    What other Component class methods related to focus have you found and tried?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. javac not recognized
    By kris972 in forum New To Java
    Replies: 7
    Last Post: 09-21-2012, 07:11 AM
  2. applet event listener
    By jobud9 in forum New To Java
    Replies: 4
    Last Post: 01-29-2012, 06:15 AM
  3. .Jar Not Recognized
    By Java Tyler in forum New To Java
    Replies: 11
    Last Post: 08-12-2010, 03:36 PM
  4. Javac not recognized
    By OscarLiu in forum New To Java
    Replies: 8
    Last Post: 06-29-2010, 03:10 PM
  5. Javac is not recognized?
    By Java idiot in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 06:54 PM

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
  •