2014-03-28 94 views

回答

2

的下面是我的解決辦法來限制一個文本框的長度。 我不會推薦使用偵聽器(在文本屬性或長度屬性上)的解決方案,它們在所有情況下都不正確(就我所見)。 我創建了一個最大長度的HTML輸入文本,並將其與我在JavaFX中的文本字段進行比較。在這兩種情況下,我都有與粘貼操作(Ctrl + V),取消操作(Ctrl + Z)相同的行爲。這裏的目標是在修改文本字段之前檢查文本是否有效。 我們可以對數字文本字段使用類似的方法。

import java.util.Objects; 
import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.scene.control.TextField; 

public class LimitedTextField extends TextField { 

    private final IntegerProperty maxLength; 

    public LimitedTextField() { 
     super(); 
     this.maxLength = new SimpleIntegerProperty(-1); 
    } 

    public IntegerProperty maxLengthProperty() { 
     return this.maxLength; 
    } 

    public final Integer getMaxLength() { 
     return this.maxLength.getValue(); 
    } 

    public final void setMaxLength(Integer maxLength) { 
     Objects.requireNonNull(maxLength, "Max length cannot be null, -1 for no limit"); 
     this.maxLength.setValue(maxLength); 
    } 

    @Override 
    public void replaceText(int start, int end, String insertedText) { 
     if (this.getMaxLength() <= 0) { 
      // Default behavior, in case of no max length 
      super.replaceText(start, end, insertedText); 
     } 
     else { 
      // Get the text in the textfield, before the user enters something 
      String currentText = this.getText() == null ? "" : this.getText(); 

      // Compute the text that should normally be in the textfield now 
      String finalText = currentText.substring(0, start) + insertedText + currentText.substring(end); 

      // If the max length is not excedeed 
      int numberOfexceedingCharacters = finalText.length() - this.getMaxLength(); 
      if (numberOfexceedingCharacters <= 0) { 
       // Normal behavior 
       super.replaceText(start, end, insertedText); 
      } 
      else { 
       // Otherwise, cut the the text that was going to be inserted 
       String cutInsertedText = insertedText.substring(
         0, 
         insertedText.length() - numberOfexceedingCharacters 
       ); 

       // And replace this text 
       super.replaceText(start, end, cutInsertedText); 
      } 
     } 
    } 
} 

測試使用JavaFX 8和Java 8u45

8

您不能直接設置字符數限制。但是,你可以添加一個listenerlengthProperty()文本字段

import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.scene.Scene; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class TextFieldLimit extends Application { 
    private static final int LIMIT = 10; 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(final Stage primaryStage) { 

     final TextField textField = new TextField(); 
     textField.lengthProperty().addListener(new ChangeListener<Number>() { 

      @Override 
      public void changed(ObservableValue<? extends Number> observable, 
        Number oldValue, Number newValue) { 
       if (newValue.intValue() > oldValue.intValue()) { 
        // Check if the new character is greater than LIMIT 
        if (textField.getText().length() >= LIMIT) { 

         // if it's 11th character then just setText to previous 
         // one 
         textField.setText(textField.getText().substring(0, LIMIT)); 
        } 
       } 
      } 
     }); 

     VBox vbox = new VBox(20); 
     vbox.getChildren().add(textField); 

     Scene scene = new Scene(vbox, 400, 300); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
} 
+4

我建議'textField.setText(textField.getText()子串(0,LIMIT)。);'。否則,如果用戶使用複製並粘貼來轉儲非常大的字符串,它會反覆調用偵聽器,一次減少文本中的一個字符。 –

+0

謝謝指出!我編輯了答案! – ItachiUchiha

+0

太好了。非常感謝。 –

0

這是一個更好的辦法來完成這項工作的一個通用的文本字段:

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); 
      } 
     } 
    }); 
} 

完美的作品,除了那撤消錯誤。

1

這是一個非常簡單的解決方案,似乎爲我工作。

textfield.setOnKeyTyped(event ->{ 
     int maxCharacters = 5; 
     if(tfInput.getText().length() > maxCharacters) event.consume(); 
    }); 
0

我用一個簡單的調用ChangeListener,在那裏我測試條件來執行停止。

textFild.addListener((observable, oldValue, newValue) -> { 
    if (newValue.length() == MAX_SIZE) { 
     textField.setText(oldValue); 
    } 
});