i need to erase data in jlabel via jtextfield whenever i pressed backspace or delete from current position.I figure out how to add data in jlable(numeric data) but don't know how to erase it or edit it.
//this is my code
String str = "";
private void jTextField1KeyPressed (java.awt.event.KeyEvent evt) {
char ch=evt.getKeyChar();
if(Character.isDigit(ch)
str += ch;
jLabel2.setText(str);
}
}
DocumentListener
instead of aKeyListener
, it will be able to detect when the user pastes text into the field and/or is changed programmaticallyDocument
is updated, get the text from the field and set the labels text. There is little benefit in trying to update anotherString
when the information is already available in the field/Document
. If you "really" have to, use aStringBuilder
instead, it's more efficient and is mutableFor example