INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

KeyListener add to a JPanel in Applet Problem

KeyListener add to a JPanel in Applet Problem

KeyListener add to a JPanel in Applet Problem

(OP)
Hello,

I'm doing an arithmetic expression calculator as an applet and I have encountered a problem... It's not crucial that this problem would be solved but if it can be solved it would make my applet better. So, I'm relatively new to Java (Java only) and I'd like to add a KeyListener so that when I press a numeric button, that number would appear in a textfield I've got to make the calculations afterwards.
I have 2 classes, one is :
class ExpressionCalculatorApplet extends Applet { ... } in which I have the method init and another class,
ExpressionCalculatorG extends JPanel implements ActionListener, KeyListener {...}

I have added the unimplemented methods and then have changed them.
So far I wanted only to see it works so that when I press a certain key that key would appear as the textfield's text.

public void keyTyped(KeyEvent e) {
expField.setText("" + e.getKeyChar());
expField.repaint();
}

where ExpField is that textfield I've been speaking of.
In ExpressionCalculatorS I have multiple panels, I can add KeyListeners to them? I wrote this :
this.addKeyListener(this); in the ExpressionCalculatorG class. Still doesn't work... any suggestions, please of what could be wrong?

Thank you,
Vasile.

RE: KeyListener add to a JPanel in Applet Problem

(OP)
I figured it out... I had to use only setFocusable(true) on the main panel in the class that contained that panel.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close