2012-12-05 60 views

回答

7

我的例子是mask

使用:

<MaskField mask="+7(DDD)DDD-DDDD"/> 
<MaskField mask="AA DDD AAA" placeholder="__ ### ___"/> 

+1

從剪貼板粘貼時甚至工程,大 – user11153

1

支持當前JavaFX的2平臺默​​認 - 沒有,但經過this link,它在JavaFX的

2

許多見解和示例代碼的表單驗證從Richard's fxexperience post限制輸入:

TextField field = new TextField() { 
    @Override public void replaceText(int start, int end, String text) { 
     // If the replaced text would end up being invalid, then simply 
     // ignore this call! 
     if (!text.matches("[a-z]")) { 
      super.replaceText(start, end, text); 
     } 
    } 

    @Override public void replaceSelection(String text) { 
     if (!text.matches("[a-z]")) { 
      super.replaceSelection(text); 
     } 
    } 
}; 

如果你想創建一個面具並創建你自己的控件,看看Richard's MoneyField,它還包括一個樣例項目和源代碼。沿着相同的路線,控制器可以在fxexperience repository中將輸入恢復爲整數,雙精度或格式化網頁顏色(例如#rrggbb)。所有這些都遵循共同的主題,他們在Control的子類中提供了一些要獲取和設置的屬性,以定義公共接口,然後還定義一個私有支持皮膚,該皮膚基於通過公共接口設置的值處理UI的呈現。

2

注意:這隻適用於JRE 1.8.0_25或更低版本。使用JRE 1.8.0_48或0_51時,每個字符輸入後插入位置始終設置爲0。

不,這不是在標準JavaFX中實現的。你需要使用一些庫或自己做。

這是我對文本字段的靜態掩碼的實現。它適用於日期,電話和其他類型的靜態口罩:

/** 
* Adds a static mask to the specified text field. 
* @param tf the text field. 
* @param mask the mask to apply. 
* Example of usage: addMask(txtDate, "// "); 
*/ 
public static void addMask(final TextField tf, final String mask) { 
    tf.setText(mask); 
    addTextLimiter(tf, mask.length()); 

    tf.textProperty().addListener(new ChangeListener<String>() { 
     @Override 
     public void changed(final ObservableValue<? extends String> ov, final String oldValue, final String newValue) { 
      String value = stripMask(tf.getText(), mask); 
      tf.setText(merge(value, mask)); 
     } 
    }); 

    tf.setOnKeyPressed(new EventHandler<KeyEvent>() { 
     @Override 
     public void handle(final KeyEvent e) { 
      int caretPosition = tf.getCaretPosition(); 
      if (caretPosition < mask.length()-1 && mask.charAt(caretPosition) != ' ' && e.getCode() != KeyCode.BACK_SPACE && e.getCode() != KeyCode.LEFT) { 
       tf.positionCaret(caretPosition + 1); 
      } 
     } 
    }); 
} 

static String merge(final String value, final String mask) { 
    final StringBuilder sb = new StringBuilder(mask); 
    int k = 0; 
    for (int i = 0; i < mask.length(); i++) { 
     if (mask.charAt(i) == ' ' && k < value.length()) { 
      sb.setCharAt(i, value.charAt(k)); 
      k++; 
     } 
    } 
    return sb.toString(); 
} 

static String stripMask(String text, final String mask) { 
    final Set<String> maskChars = new HashSet<>(); 
    for (int i = 0; i < mask.length(); i++) { 
     char c = mask.charAt(i); 
     if (c != ' ') { 
      maskChars.add(String.valueOf(c)); 
     } 
    } 
    for (String c : maskChars) { 
     text = text.replace(c, ""); 
    } 
    return text; 
} 

public static void addTextLimiter(final TextField tf, final int maxLength) { 
    tf.textProperty().addListener(new ChangeListener<String>() { 
     @Override 
     public void changed(final ObservableValue<? extends String> ov, final String oldValue, final String newValue) { 
      if (tf.getText().length() > maxLength) { 
       String s = tf.getText().substring(0, maxLength); 
       tf.setText(s); 
      } 
     } 
    }); 
} 

參見: JavaFX 2.2 TextField maxlength

0
public class NumberTextField extends TextField { 
    private int maxLenght; 

    public NumberTextField(int maxLenght) { 
     super(); 
     this.maxLenght = maxLenght; 
    } 

    @Override 
    public void replaceText(int start, int end, String text) { 
     if (validate(text)) { 
      super.replaceText(start, end, text); 
     } 
    } 

    @Override 
    public void replaceSelection(String text) { 
     if (validate(text)) { 
      super.replaceSelection(text); 
     } 
    } 

    private boolean validate(String text) { 
     if (this.getText() != null) { 
     } 
     boolean status = ("".equals(text) || text.matches("[0-9]")); 
     if (this.getText() == null) { 
      return status; 
     } else { 
      return (status && this.getText().length() < maxLenght); 
     } 
    } 
} 
2

我有同樣的需求。我創建了這個字段,將其稱爲SpecialTextField,並推送到GitHub。例子也在那裏。希望這個幫助。

0

在某些情況下,我會驗證文本屬性:

myTextField 
    .textProperty() 
    .addListener(
    (obs, oldVal, newVal) -> 
     { 
      if(!newVal.matches("\\d+")) 
       textField.setText(oldV); 
     }); 

倒黴:textField.setText(oldV);將再次輸入相同的功能,測試不必要如果OLDVAL匹配。

如果在將此偵聽器添加到TextField之前TextField變爲不匹配的值,則輸入一個不匹配的新值將導致循環!

爲了避免這種情況,這將是更安全的寫:

String acceptableValue = "0"; 
myTextField 
    .textProperty() 
    .addListener(
    (obs, oldVal, newVal) -> 
     { 
      if(!newVal.matches("\\d+")) 
       textField.setText(oldVal.matches("\\d+") ? oldV : acceptableValue); 
     }); 
0

我寫的,它擴展了TextField和敷面膜類。

package com.model; 

import java.text.NumberFormat; 
import java.util.Locale; 

/** 
* ATENTION 
* DO NOT FORGUET TO IMPORT IN FXML 
* <?import com.view.TextFieldMoney?> 
* 
* */ 

import javafx.scene.control.TextField; 

public class TextFieldMoney extends TextField { 
    private int maxlength; 
    private String valor = ""; 

    public TextFieldMoney() { 
     this.maxlength = 11; 
    } 

    public void setMaxlength(int maxlength) { 
     this.maxlength = maxlength; 
    } 

    @Override 
    public void replaceText(int start, int end, String text) { 
     // Delete or backspace user input. 
     if (getText() == null || getText().equalsIgnoreCase("")) { 
      valor = ""; 
     } 
     if (text.equals("")) { 
      super.replaceText(start, end, text); 
     } else{ 


      text = text.replaceAll("[^0-9]", ""); 
      valor += text; 

      super.replaceText(start, end, text); 
      if (!valor.equalsIgnoreCase("")) 
       setText(formata(valor)); 
     } 
    } 

    @Override 
    public void replaceSelection(String text) { 
     // Delete or backspace user input. 
     if (text.equals("")) { 
      super.replaceSelection(text); 
     } else if (getText().length() < maxlength) { 
      // Add characters, but don't exceed maxlength. 
      // text = MascaraFinanceira.show(text); 
      if (text.length() > maxlength - getText().length()) { 
       // text = MascaraFinanceira.show(text); 
       text = text.substring(0, maxlength - getText().length()); 
      } 
      super.replaceSelection(text); 
     } 
    } 

    /* 
    *Return the number without money mask 
    **/ 

    public String getCleanValue(){ 
     String cleanString = getText().replaceAll("[^0-9]", ""); 
     Double cleanNumber = new Double(cleanString); 
     return String.valueOf(cleanNumber/100); 
    } 

    private String formata(Double valor) { 
     Locale locale = new Locale("pt", "BR"); 
     NumberFormat nf = NumberFormat.getInstance(locale); 
     nf.setMaximumFractionDigits(2); 
     nf.setMinimumFractionDigits(2); 

     return nf.format(valor); 
    } 

    public String formata(String valor) { 
     double v = new Double(valor); 
     return formata(v/100); 
    } 

} 

而在FXML哪裏是

<TextField fx:id="valorTextField" GridPane.columnIndex="2" GridPane.rowIndex="2" /> 

<TextFieldMoney fx:id="valorTextField" GridPane.columnIndex="2" GridPane.rowIndex="2" />