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);
        }
} 
 
                        
DocumentListenerinstead of aKeyListener, it will be able to detect when the user pastes text into the field and/or is changed programmaticallyDocumentis updated, get the text from the field and set the labels text. There is little benefit in trying to update anotherStringwhen the information is already available in the field/Document. If you "really" have to, use aStringBuilderinstead, it's more efficient and is mutableFor example