首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数字格式的JavaFX 8绑定

数字格式的JavaFX 8绑定
EN

Stack Overflow用户
提问于 2015-08-24 02:15:38
回答 2查看 8K关注 0票数 6

请有人向我展示一个Bindings.bindBidirectional的例子,其中一个textfield绑定到一个双值,textfield被格式化为零小数位。我有这样的装订:

代码语言:javascript
复制
Bindings.bindBidirectional(xProperty, sp.layoutXProperty(), converter);

其中xProperty是StringProperty,sp.layoutXProperty是DoubleProperty。

我尝试过许多不同的转换器,最后决定:

代码语言:javascript
复制
NumberFormat nf = NumberFormat.getInstance();    
StringConverter<Number> converter = new NumberStringConverter(nf);

然后我试着:

代码语言:javascript
复制
nf.setParseIntegerOnly(true);

但没有结果。这只是实现这一结果的许多尝试之一。这可能是直截了当的,但是关于用一种格式绑定不同属性的信息似乎很少,或者我忽略了显而易见的内容吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-24 12:29:57

查看这段代码,看看它是否解决了您的问题:

代码语言:javascript
复制
public class BindingTest {
    static Property<String> sp;
    static Property<Double> dp;

    public static void main(String[] args) {
        sp = new SimpleStringProperty();
        dp = new SimpleObjectProperty<>();

        StringConverter<Double> sc = new StringConverter<Double>() {
            @Override
            public String toString(Double object) {
                if (object != null)
                    return Integer.toString((int) Math.round(object.doubleValue()));
                else
                    return null;
            }

            @Override
            public Double fromString(String string) {
                Double d = Double.parseDouble(string);
                sp.setValue(Integer.toString((int) Math.round(d)));
                return d;
            }
        };

        Bindings.bindBidirectional(sp, dp, sc);

        print();
        sp.setValue("3.14");
        print();
        dp.setValue(6.18);
        print();
    }

    public static void print() {
        System.out.println("String: " + sp.getValue() + "\t" + "Double: " + dp.getValue());
    }
}

输出:

代码语言:javascript
复制
String: null    Double: null
String: 3   Double: 3.14
String: 6   Double: 6.18
票数 5
EN

Stack Overflow用户

发布于 2015-08-24 14:44:29

可能有不同的方法将双值转换为整数,它们在转换时使用不同的圆。这个答案与其他的类似,有一个额外的过滤器,只允许文本字段中的整数值:

代码语言:javascript
复制
@Override
public void start( Stage stage )
{
    TextField field = new TextField();
    DoubleProperty doubleProperty = new SimpleDoubleProperty();
    Label label = new Label();
    label.textProperty().bind( doubleProperty.asString() );

    DecimalFormat format = new DecimalFormat();
    format.setParseIntegerOnly( true );  // used while parsing the input text of textfield
    format.setMaximumFractionDigits( 0 );  // used while formatting the doubleProperty's bound value as output text of textfield
    format.setGroupingUsed( false );  // disable thousand grouping
    format.setRoundingMode( RoundingMode.UP );  // set rounding mode

    Bindings.bindBidirectional( field.textProperty(), doubleProperty, new StringConverter<Number>()
    {
        @Override
        public String toString( Number object )
        {
            return object == null ? "" : format.format( object );
        }


        @Override
        public Number fromString( String string )
        {
            return (string != null && !string.isEmpty()) ? Double.valueOf( string ) : null;
        }
    } );

    // apply filter to allow only integer values
    field.setTextFormatter( new TextFormatter<>( c ->
    {
        if ( c.getControlNewText().isEmpty() )
        {
            return c;
        }

        ParsePosition parsePosition = new ParsePosition( 0 );
        Object object = format.parse( c.getControlNewText(), parsePosition );

        if ( object == null || parsePosition.getIndex() < c.getControlNewText().length() )
        {
            return null;
        }
        else
        {
            return c;
        }

    } ) );

    Button b = new Button( "+1.6" );
    b.setOnAction( ( e ) ->
    {
        doubleProperty.set( doubleProperty.get() + 1.6 );
    } );

    Scene scene = new Scene( new VBox( 5 ) );
    stage.setWidth( 450 );
    stage.setHeight( 250 );
    (( VBox ) scene.getRoot()).getChildren().addAll( field, label, b );
    stage.setScene( scene );
    stage.show();
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32173624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档