Reputation: 4151
I have a JTextPane which is bind with KeyReleased Event. My Goal is that whatever user typed in that TextPane It will print some character.
{'I', 'N', 'V', 'A', 'L', 'I', 'D'};
Above character printed every keyReleased Event. If the user pressed any character
then JTextPane Shows 'I'
after next event shows 'N' like wise.
If I pressed one by one its working properly. But my problem is that If I typed rapidly I got the typed character.
How can I hide these characters?
KeyPressedEvent Code:
fooString
is char array with 13 char.
count++; //count of event
if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
{
int option = JOptionPane.showConfirmDialog(this, "Do you want to exit");
if(option == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
else if(evt.getKeyCode() == KeyEvent.VK_BACK_SPACE)
{
if(this.txtPane.getText().length() != 0)
count--;
}
else
{
/*Get the value and show fooString value */
try {
String val = this.txtPane.getText();
int rem = count % fooString.length;
//System.out.println(val.substring(0, val.length()-1));
txtPane.setText(val.substring(count-1, val.length()-1));
if(rem == 0)
rem = foolString.length;
if(count % 26 == 0)
{
txtPane.setText(val.substring(0, val.length()-1)
+ fooString[rem-1] +"\n");
lineCount++;
count = 0;
}
else
txtPane.setText(val.substring(0, val.length()-1) + fooString[rem-1]);
} catch (Exception ex) {
Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
}
}
Cheers..
Upvotes: 1
Views: 141
Reputation: 109823
don't to use KeyListener for JTextComponents
use DocumentListener
My Goal is that whatever user typed in that TextPane It will print some character.
{'I', 'N', 'V', 'A', 'L', 'I', 'D;}
) with Pattern
Upvotes: 3