简体   繁体   中英

How to clear only one value from text field in one press on button in java swing?

I want to clear only one value per press from text field

public void actionPerformed(ActionEvent e){

       String s= textfield.getText();
       textfield.setText(s[s.length()-1]);

Don't replace the entire string each time.

Instead you can update the Document directly to only remove the last character:

try
{
    Document doc = textField.getDocument();
    doc.remove(doc.getLength() - 1, 1);
}
catch(BadLocationException ble) { ble.getStackTrace(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM