java - How do I create a JTextField that ignores user input but lets components modify text? -
here's i'd proof of concept: want frame text field , button on it. user types text field ignored, if click button, sets text on text field. how this?
i've done research ahead of time , come solution i'll post answer. concern there's easier way this. i'm asking question find out if there's better way.
here's screenshot of reason i'm trying build proof of concept:
i'm trying build this. when user enters text, want ignore saying, capture it, , put own interpretation. example, if user pressed control+y, want this:
i don't want make jtextfield un-editable, because grays out box , hides cursor. want people know should click on text field , type.
if want maintain cursor, can intercept keystrokes described here.
code above link:
mytextfield.addkeylistener(new keyadapter() { @override public void keytyped(keyevent e) { char c = e.getkeychar(); if (!character.isdigit(c)) { e.consume(); // stop event propagating. } } });
alternatively, can disable entry , restore background color enough, lose cursor:
textfield.seteditable(false); textfield.setbackground(color.white);
Comments
Post a Comment