java - display control character as space in TextField -
i have display string in component textbox,textfile in javafx. string can contain control,non printable characters (i.e characters in ascii < 32). when display string on console println() control characters replacing space( in view) because can copy , past same string still control characters.by default non printable characters skiped displaying. want receive same resultat display/enter special characters in javafx's component have place in java console, possible??
feel free use and/or edit code made:
import java.awt.toolkit; import java.awt.datatransfer.clipboard; import java.awt.datatransfer.dataflavor; import java.awt.datatransfer.stringselection; import java.awt.datatransfer.unsupportedflavorexception; import java.io.ioexception; import javax.swing.jtextfield; import javax.swing.text.attributeset; import javax.swing.text.badlocationexception; import javax.swing.text.plaindocument; /** * text field control characters displayed same optional * char. default replacement char space. */ public class mytextfield extends jtextfield { private static final long serialversionuid = -4027593772585498003l; private static final clipboard clipboard = toolkit.getdefaulttoolkit().getsystemclipboard(); //string control characters aren't replaced private string ctrltext; //char replacing control characters private char replacementchar; public mytextfield() { this(null, 0); } public mytextfield(int columns) { this(null, columns); } public mytextfield(string text) { this(text, 0); } public mytextfield(string text, int columns) { ctrltext = (text == null) ? "" : text; setreplacementchar(' '); setdocument(new customdocument()); settext(text); setcolumns(columns); } /** * sets char replace control characters. * * @param ch char replace control characters */ public void setreplacementchar(char ch) { replacementchar = ch; } @override public void cut() { int start = getselectionstart(); int end = getselectionend(); //copy clipboard string data = ctrltext.substring(start, end); clipboard.setcontents(new stringselection(data), null); //set new text string newtext = ctrltext.substring(0, start) + ctrltext.substring(end); settext(newtext); } @override public void copy() { int start = getselectionstart(); int end = getselectionend(); //copy clipboard string data = ctrltext.substring(start, end); clipboard.setcontents(new stringselection(data), null); } @override public void paste() { int start = getselectionstart(); int end = getselectionend(); //get data clipboard string data = null; try { data = (string)clipboard.getdata(dataflavor.stringflavor); } catch (unsupportedflavorexception e) {} catch (ioexception e) {} //set new text string newtext = ctrltext.substring(0, start) + data + ctrltext.substring(end); settext(newtext); } /** * replaces control characters replacement char. * * @param text string control characters replaced * @return resulting string */ private string replacectrlchars(string text) { if(text == null) return null; return text.replaceall("\\p{cntrl}", "" + replacementchar); } /** * document control characters displayed replacement char. */ private class customdocument extends plaindocument { @override public void remove(int offs, int len) throws badlocationexception { //remove ctrltext ctrltext = ctrltext.substring(0, offs) + ctrltext.substring(offs + len); //remove text field super.remove(offs, len); } @override public void insertstring(int offs, string str, attributeset a) throws badlocationexception { //insert ctrltext ctrltext = ctrltext.substring(0, offs) + str + ctrltext.substring(offs); //insert string replaced chars text field super.insertstring(offs, replacectrlchars(str), a); } private static final long serialversionuid = -6187351778620427126l; } }
it's text field control characters (those ascii < 32 or == 127), displayed same, optional char
.
the default replacement char
space, can set displayable char
calling setreplacementchar
. i'd use question mark.
Comments
Post a Comment