2013-04-05 69 views
1

我必須在JavaFX中的某個組件(如TextBox,TextFile)中顯示String。該字符串可以包含控件,非打印字符(即ASCII < 32中的所有字符)。當我通過println()在控制檯上顯示我的字符串時,控制字符將被空間替換(僅在視圖中),因爲我可以複製並通過仍然帶有控制字符的相同字符串。默認情況下,不可打印字符被顯示。我想要得到相同的結果,以便在任何JAvaFx組件中顯示/輸入特殊字符,如它在Java控制檯中的位置,這是可能的?顯示控制字符作爲TextField中的空格

回答

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是空間,但你可以通過調用setReplacementChar將其設置爲任何可顯示char。我可能會用一個問號。