在TextField中将控制字符显示为空格

时间:2013-04-05 17:32:58

标签: java swing javafx-2

我必须在某些组件中显示String,如TextBox,JavaFX中的TextFile。该字符串可以包含控制,不可打印的字符(即ASCII< 32中的所有字符)。当我通过println()在控制台上显示我的字符串时,控制字符将被空格替换(仅在视图中),因为我可以复制并通过相同的字符串仍然使用控制字符。默认不可打印的字符可以显示。我希望得到相同的结果来显示/输入任何JAvaFx组件中的特殊字符,就像它在Java控制台中一样,它有可能吗??

1 个答案:

答案 0 :(得分:0)

随意使用和/或编辑我制作的代码:

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;

/**
 * A text field where all control characters are displayed as the same optional
 * char. The default replacement char is space.
 */
public class MyTextField extends JTextField {

    private static final long serialVersionUID = -4027593772585498003L;
    private static final Clipboard clipboard =
            Toolkit.getDefaultToolkit().getSystemClipboard();

    //String whose control characters aren't replaced
    private String ctrlText;
    //Char for 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 the char to replace the control characters.
     * 
     * @param ch  the char to replace the control characters
     */
    public void setReplacementChar(char ch) {
        replacementChar = ch;
    }

    @Override
    public void cut() {
        int start = getSelectionStart();
        int end   = getSelectionEnd();

        //Copy to 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 to 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 from 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 all control characters with the replacement char.
     * 
     * @param text  the String whose control characters are to be replaced
     * @return      the resulting String
     */
    private String replaceCtrlChars(String text) {
        if(text == null) return null;

        return text.replaceAll("\\p{Cntrl}", "" + replacementChar);
    }
    /**
     * Document where control characters are displayed as the replacement char.
     */
    private class CustomDocument extends PlainDocument {
        @Override
        public void remove(int offs, int len) throws BadLocationException {
            //Remove from ctrlText
            ctrlText = ctrlText.substring(0, offs)
                    + ctrlText.substring(offs + len);
            //Remove from text field
            super.remove(offs, len);
        }
        @Override
        public void insertString(int offs, String str, AttributeSet a)
                throws BadLocationException {
            //Insert to ctrlText
            ctrlText = ctrlText.substring(0, offs)
                    + str
                    + ctrlText.substring(offs);
            //Insert String with replaced chars to text field
            super.insertString(offs, replaceCtrlChars(str), a);
        }
        private static final long serialVersionUID = -6187351778620427126L;
    }
}

这是一个文本字段,其中控制字符(ASCII <32或== 127)显示为相同的可选char

默认替换char是空格,但您可以通过调用char将其设置为任何可显示的setReplacementChar。我可能会使用问号。