java - Empty jtextfield/jpassword delete sound -
well, working on java project requires input user. realised if user press backspace when there no characters in field, window warning sound heard. how can stop please. system windows 10 if @ behavior may different on different platforms. thank you.
behavior may different on different platforms.
yes, behaviour can different because controlled laf, should not changing it.
but understand how swing works need understand swing uses action
provided defaulteditorkit
provide editing functions of text components.
following code current "delete previous character" action (taken defaulteditkit
):
/* * deletes character of content precedes * current caret position. * @see defaulteditorkit#deleteprevcharaction * @see defaulteditorkit#getactions */ static class deleteprevcharaction extends textaction { /** * creates object appropriate identifier. */ deleteprevcharaction() { super(defaulteditorkit.deleteprevcharaction); } /** * operation perform when action triggered. * * @param e action event */ public void actionperformed(actionevent e) { jtextcomponent target = gettextcomponent(e); boolean beep = true; if ((target != null) && (target.iseditable())) { try { document doc = target.getdocument(); caret caret = target.getcaret(); int dot = caret.getdot(); int mark = caret.getmark(); if (dot != mark) { doc.remove(math.min(dot, mark), math.abs(dot - mark)); beep = false; } else if (dot > 0) { int delchars = 1; if (dot > 1) { string dotchars = doc.gettext(dot - 2, 2); char c0 = dotchars.charat(0); char c1 = dotchars.charat(1); if (c0 >= '\ud800' && c0 <= '\udbff' && c1 >= '\udc00' && c1 <= '\udfff') { delchars = 2; } } doc.remove(dot - delchars, delchars); beep = false; } } catch (badlocationexception bl) { } } if (beep) { uimanager.getlookandfeel().provideerrorfeedback(target); } } }
if don't beep need create own custom action remove beep sound. (ie. don't provide error feedback). once customize action can change single text field using:
textfield.getactionmap().put(defaulteditorkit.deleteprevcharaction, new mydeleteprevcharaction());
or can change text fields using:
actionmap = (actionmap)uimanager.get("textfield.actionmap"); am.put(defaulteditorkit.deleteprevcharaction, new mydeleteprevcharaction());
Comments
Post a Comment