Как внедрить числовое поле в Javafx 2.0?
-
28-10-2019 - |
Вопрос
Мне нужно вставить числовое поле в мой пользовательский интерфейс. Поэтому мне нужно проверить события ключей в текстовом поле, чтобы проверить, является ли входной символ номером. Я создал класс, расширив Textfield. Если в классе Textfield есть метод, который обрабатывает KeyEvents, я могу просто переоценить этот метод с подходящими для номера поле. Любые идеи?
Спасибо
Решение 4
Нашел решение. :)
public class NumFieldFX extends TextField {
public NumFieldFX() {
this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
public void handle( KeyEvent t ) {
char ar[] = t.getCharacter().toCharArray();
char ch = ar[t.getCharacter().toCharArray().length - 1];
if (!(ch >= '0' && ch <= '9')) {
System.out.println("The char you entered is not a number");
t.consume();
}
}
});
}
}
Другие советы
Обновление 27 мая 2016 г.
Java 8U40 представил Текстовая форматада Класс, который является рекомендуемым способом выполнения этой функциональности (хотя решение, предоставленное в этом ответе, все равно будет работать). Для получения дополнительной информации см. Уве ответ, Ответ Хасана и другие ответы, упоминающие текстовую форму, на следующий вопрос:
Существует также это решение из другого ответа на этот вопрос, который я не пробовал, но выглядит хорошо, и модератор Stackoverflow удален:
TextField numberField = new TextField();
numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
Приведенный выше код пропускает фильтр unaryoperator для текстового форматератера, который вам обычно также требуется (в противном случае поле не будет отображать ввод пользователя только в форматированном значении, он просто позволит вам контролировать неформатированное значение через свойство значения текстовых формат. ) Чтобы расширить решение для использования фильтра, можно использовать подобный код ниже:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.stage.Stage;
import javafx.util.converter.NumberStringConverter;
import java.text.ParsePosition;
import java.util.function.UnaryOperator;
public class NumberConverterFieldTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TextField numberField = new TextField();
NumberStringFilteredConverter converter = new NumberStringFilteredConverter();
final TextFormatter<Number> formatter = new TextFormatter<>(
converter,
0,
converter.getFilter()
);
numberField.setTextFormatter(formatter);
formatter.valueProperty().addListener((observable, oldValue, newValue) ->
System.out.println(newValue)
);
stage.setScene(new Scene(numberField));
stage.show();
}
class NumberStringFilteredConverter extends NumberStringConverter {
// Note, if needed you can add in appropriate constructors
// here to set locale, pattern matching or an explicit
// type of NumberFormat.
//
// For more information on format control, see
// the NumberStringConverter constructors
// DecimalFormat class
// NumberFormat static methods for examples.
// This solution can instead extend other NumberStringConverters if needed
// e.g. CurrencyStringConverter or PercentageStringConverter.
public UnaryOperator<TextFormatter.Change> getFilter() {
return change -> {
String newText = change.getControlNewText();
if (newText.isEmpty()) {
return change;
}
ParsePosition parsePosition = new ParsePosition( 0 );
Object object = getNumberFormat().parse( newText, parsePosition );
if ( object == null || parsePosition.getIndex() < newText.length()) {
return null;
} else {
return change;
}
};
}
}
}
При запуске приведенного выше примера отредактируйте поле ввода и нажмите клавишу Enter, чтобы увидеть обновленное значение ( System.out
При изменении).
Для учебника см.:
- Javaworld TextFormatter статья (Остерегайтесь раздражающих всплывающих рекламных объявлений).
Это то же решение, которое ссылается на URS, однако я просто поместил его в полностью исполняемую программу, чтобы дать пример в контексте и изменил регулярное выражение (добавив *
в конце) так что копирование и вставка работает, и у него нет проблемы, на которую ссылается Улук. Решение кажется довольно простым и, вероятно, будет достаточно для большинства целей:
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class NumericTextFieldTest extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
TextField numberField = new TextField() {
@Override public void replaceText(int start, int end, String text) {
if (text.matches("[0-9]*")) {
super.replaceText(start, end, text);
}
}
@Override public void replaceSelection(String text) {
if (text.matches("[0-9]*")) {
super.replaceSelection(text);
}
}
};
stage.setScene(new Scene(numberField));
stage.show();
}
}
Альтернативные решения
Вы также можете быть заинтересованы в моем альтернативном решении в Javafx Пример привязки значения слайдера с редактируемым текстовым полем. Анкет В этом решении я расширяю Textfield, чтобы разоблачить IntegerProperty на поле для простых целей привязки. Альтернативное решение аналогично тому, что описано исходным постером в их обновленном вопросе (т.е. фильтр событий добавляется для ограничения входных данных из ключевых событий), но дополнительно подготовитель добавляется в текстовое свойство Textfield, чтобы гарантировать, что копия и вставленные значения принимаются только в том случае, если они числовые.
Есть некоторые другие решения для этого вопроса в ветке форума Javafx Числовое текстовое поле в Javafx 2.0? который включает в себя ссылку на Номерные поля из контроля FxExperience.
Есть совет на FxExperience Это касается такой проблемы. Чтобы перефразировать, вы расширяете TextField
и переопределить replaceText()
а также replaceSelection()
Методы, фильтрация всего входа, который не является числом.
После реализации оба метода должны следовать этому шаблону:
if (!newText.matches("[0-9]")) {
super.call(allParams)
}
Вот поле Customext, которое я написал. Он обрабатывает только входные цифры, а также максимум. Это пользовательский элемент управления, который можно использовать в FXML, а также свойства могут быть установлены в самом FXML.
package fxml;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.IntegerPropertyBase;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.TextField;
public class CustomTextField extends TextField {
/**
* numericOnly property if set, will allow accept only numeric input.
*/
private BooleanProperty numericOnly = new SimpleBooleanProperty(this,
"numericOnly", false);
public final boolean isNumericOnly() {
return numericOnly.getValue();
}
public final void setNumericOnly(boolean value) {
numericOnly.setValue(value);
}
public final BooleanProperty numericOnlyProperty() {
return numericOnly;
}
/**
* maxSize property , determines the maximum size of the text that can be
* input.
*/
public IntegerProperty maxSize = new IntegerPropertyBase(1000) {
@Override
public String getName() {
return "maxSize";
}
@Override
public Object getBean() {
return CustomTextField.this;
}
};
public final IntegerProperty maxSizeProperty() {
return maxSize;
};
public final int getMaxSize() {
return maxSize.getValue();
}
public final void setMaxSize(int value) {
maxSize.setValue(value);
}
/**
* this method is called when user inputs text into the textField
*/
@Override
public void replaceText(int start, int end, String text) {
if (numericOnly.getValue() && !text.equals("")) {
if (!text.matches("[0-9]")) {
return;
}
}
if (getText().length() < getMaxSize() || text.equals("")) {
super.replaceText(start, end, text);
}
}
/**
* this method is called when user pastes text into the textField
*/
@Override
public void replaceSelection(String text) {
if (numericOnly.getValue() && !text.equals("")) {
if (!text.matches("[0-9]+")) {
return;
}
}
super.replaceSelection(text);
if (getText().length() > getMaxSize()) {
String maxSubString = getText().substring(0, getMaxSize());
setText(maxSubString);
positionCaret(getMaxSize());
}
}
}
Для ряда текстовых файлов (включать десятичную точку)
Arrays.asList(txtLongitude, txtLatitude, txtAltitude, txtSpeed, txtOrientation).forEach(textField ->
textField.textProperty().addListener((observable, oldValue, newValue) ->
textField.setText(newValue.matches("^[0-9]*\\.?[0-9]*$") ? newValue : oldValue)
));
Объединенное решение Baijifeilong и Ajay Prakash для поддержки десятичных знаков
package com.mazeworks.cloudhms.view.components;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.IntegerPropertyBase;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.TextField;
public class NumericTextField extends TextField {
/**
* numericOnly property if set, will allow accept only numeric input.
*/
private BooleanProperty numericOnly = new SimpleBooleanProperty(this,
"numericOnly", false);
public final boolean isNumericOnly() {
return numericOnly.getValue();
}
public final void setNumericOnly(boolean value) {
numericOnly.setValue(value);
}
public final BooleanProperty numericOnlyProperty() {
return numericOnly;
}
/**
* maxSize property, determines the maximum size of the text that
can be
* input.
*/
public IntegerProperty maxSize = new IntegerPropertyBase(1000) {
@Override
public String getName() {
return "maxSize";
}
@Override
public Object getBean() {
return NumericTextField.this;
}
};
public final IntegerProperty maxSizeProperty() {
return maxSize;
}
;
public final int getMaxSize() {
return maxSize.getValue();
}
public final void setMaxSize(int value) {
maxSize.setValue(value);
}
/**
* this method is called when user inputs text into the textField
*/
@Override
public void replaceText(int start, int end, String text) {
if (numericOnly.getValue() && !text.equals("")) {
if (!text.matches("^[0-9]*\\.?[0-9]*$")) {
return;
}
}
if (getText().length() < getMaxSize() || text.equals("")) {
super.replaceText(start, end, text);
}
}
/**
* this method is called when user pastes text into the textField
*/
@Override
public void replaceSelection(String text) {
if (numericOnly.getValue() && !text.equals("")) {
if (!text.matches("^[0-9]*\\.?[0-9]*$")) {
return;
}
}
super.replaceSelection(text);
if (getText().length() > getMaxSize()) {
String maxSubString = getText().substring(0, getMaxSize());
setText(maxSubString);
positionCaret(getMaxSize());
}
}
}
Наследовать от Текстовое поле и переопределить Replacetext Таким образом, чтобы получить двойное значения только Textfield:
@Override
public void replaceText(int start, int end, String text) {
String preText = getText(0, start);
String afterText = getText(end, getLength());
String toBeEnteredText = preText + text + afterText;
// Treat the case where the user inputs proper text and is not inputting backspaces or other control characters
// which would be represented by an empty text argument:
if (!text.isEmpty() && text.matches("\\d|\\.")) {
Logger.getAnonymousLogger().info("Paring non-empty.");
try {
Logger.getAnonymousLogger().info("Parsing " + toBeEnteredText);
Double.parseDouble(toBeEnteredText);
super.replaceText(start, end, text);
} catch (Exception ignored) {
}
}
// If the user used backspace or del, the result text is impossible to not parse as a Double/Integer so just
// enter that text right ahead:
if (text.isEmpty()) {
super.replaceText(start, end, text);
}
}