Extracting a value from an inner anonymous class

  • tj
  • Newbie
  • Newbie
  • tj
  • Posts: 6

Post 3+ Months Ago

Hi guys,

Have u got any idea how to extract a variable from an anonymous inner class.

I have two jtextareas: t1, t2
t1 is implemented with a key listener, so that when a key is typed in it;
a cipher of it appears on t2.

Here's the code:
Code: [ Select ]
t1.addKeyListener(new KeyListener(){
public void keyTyped (KeyEvent e)
{
char keyChar = e.getKeyChar();
keyChar = Character.toUpperCase(keyChar);
jta2_char = encrypt(keyChar); // encrypt is another method outside the constructor
}// end keyTyped
public void keyPressed(KeyEvent e){}// end keyPressed
public void keyReleased(KeyEvent e) {}// end keyReleased
}// end KeyListener
);// end addKeyListener
  1. t1.addKeyListener(new KeyListener(){
  2. public void keyTyped (KeyEvent e)
  3. {
  4. char keyChar = e.getKeyChar();
  5. keyChar = Character.toUpperCase(keyChar);
  6. jta2_char = encrypt(keyChar); // encrypt is another method outside the constructor
  7. }// end keyTyped
  8. public void keyPressed(KeyEvent e){}// end keyPressed
  9. public void keyReleased(KeyEvent e) {}// end keyReleased
  10. }// end KeyListener
  11. );// end addKeyListener


now how would I get the encrypted characters onto text area 2 (t2)?

Please Help !

Regards
Dimel :)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

Inner classes have access to the variables of the enclosing class instance, so assuming that jta2_char and t2 are part of the enclosing outer class you should be able to append or set t2's text directly from the inner class.

Code: [ Select ]
jta2_char = encrypt(keyChar);
t2.append("" + jta2_char));
  1. jta2_char = encrypt(keyChar);
  2. t2.append("" + jta2_char));

or if you don't need to save the encrypted char in a separate variable:
Code: [ Select ]
t2.append("" + encrypt(keyChar));


The blank string is needed because JTextArea's append method takes a string as an argument, so a blank string plus a char will concatenate the char to the blank string and pass it as a one character string.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 88 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.