仅在微调控件中插入数字 [英] Insert only numbers in Spinner Control

查看:19
本文介绍了仅在微调控件中插入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 8u40 中测试了 Spinner 控件

I tested Spinner control in Java 8u40

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class MainApp extends Application
{
    public static void main(String[] args)
    {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage)
    {
        final Spinner spinner = new Spinner();

        spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 10000));
        spinner.setEditable(true);

        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(10));

        int row = 0;

        grid.add(new Label("Spinner:"), 0, row);
        grid.add(spinner, 1, row);

        Scene scene = new Scene(grid, 350, 300);

        stage.setTitle("Hello Spinner");
        stage.setScene(scene);
        stage.show();
    }
}

如何只能在微调控件字段中插入数字?

How I can only insert number into the spinner control field?

现在我可以插入数字和文本了.有什么例子可以作为例子吗?

Now I can insert numbers and text. Is there any example that can be used as example?

推荐答案

不完全确定要求 - 假设您想防止输入无法解析为有效数字的字符.

Not entirely certain about the requirement - assuming that you want to prevent the input of characters that wouldn't parse to a valid Number.

如果是这样,在 Spinner 的编辑器中使用 TextFormatter 就派上用场了:有了它,您将监视文本的任何更改并接受或拒绝它.该决定封装在格式化程序的过滤器中.一个非常简单的版本(肯定还有更多的事情要做,请参阅 Swing 的 DefaultFormatter)

If so, usage of a TextFormatter in the Spinner's editor comes to the rescue: with it, you'll monitor any change of text and either accept or reject it. The decision is encapsulated inside the formatter's filter. A very simple version (there's definitely more to do, see Swing's DefaultFormatter)

// get a localized format for parsing
NumberFormat format = NumberFormat.getIntegerInstance();
UnaryOperator<TextFormatter.Change> filter = c -> {
    if (c.isContentChange()) {
        ParsePosition parsePosition = new ParsePosition(0);
        // NumberFormat evaluates the beginning of the text
        format.parse(c.getControlNewText(), parsePosition);
        if (parsePosition.getIndex() == 0 ||
                parsePosition.getIndex() < c.getControlNewText().length()) {
            // reject parsing the complete text failed
            return null;
        }
    }
    return c;
};
TextFormatter<Integer> priceFormatter = new TextFormatter<Integer>(
        new IntegerStringConverter(), 0, filter);

spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(
        0, 10000, Integer.parseInt(INITAL_VALUE)));
spinner.setEditable(true);
spinner.getEditor().setTextFormatter(priceFormatter);

这篇关于仅在微调控件中插入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆