Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KeyListener add to a JPanel in Applet Problem

Status
Not open for further replies.

Vasile1991

Programmer
May 26, 2012
7
RO
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.
 
I figured it out... I had to use only setFocusable(true) on the main panel in the class that contained that panel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top