Properties 带数字格式的JavaFX8绑定

Properties 带数字格式的JavaFX8绑定,properties,javafx,formatting,Properties,Javafx,Formatting,有人能给我举一个Bindings.BindField的例子吗?其中一个textfield绑定到一个双精度值,而textfield的格式是零位小数。我有这个约束力: Bindings.bindBidirectional(xProperty, sp.layoutXProperty(), converter); 其中,xProperty是StringProperty,sp.layoutXProperty是DoubleProperty 我尝试了许多不同的转换器,最终决定: NumberFormat n

有人能给我举一个Bindings.BindField的例子吗?其中一个textfield绑定到一个双精度值,而textfield的格式是零位小数。我有这个约束力:

Bindings.bindBidirectional(xProperty, sp.layoutXProperty(), converter);
其中,xProperty是StringProperty,sp.layoutXProperty是DoubleProperty

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

NumberFormat nf = NumberFormat.getInstance();    
StringConverter<Number> converter = new NumberStringConverter(nf);

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

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

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

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

@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();
}
@覆盖
公众假期开始(阶段)
{
TextField=newtextfield();
DoubleProperty DoubleProperty=新的SimpleDoubleProperty();
标签=新标签();
label.textProperty().bind(doubleProperty.asString());
DecimalFormat format=新的DecimalFormat();
format.setParseIntegerOnly(true);//在解析textfield的输入文本时使用
format.setMaximumFractionDigits(0);//将doubleProperty的绑定值格式化为textfield的输出文本时使用
format.setGroupingUsed(false);//禁用分组
format.setRoundingMode(RoundingMode.UP);//设置取整模式
Bindings.BindBidirective(field.textProperty()、doubleProperty、new StringConverter())
{
@凌驾
公共字符串toString(数字对象)
{
返回对象==null?”:format.format(对象);
}
@凌驾
公共编号fromString(String)
{
return(string!=null&!string.isEmpty())?Double.valueOf(string):null;
}
} );
//应用筛选器以仅允许整数值
field.setTextFormatter(新的TextFormatter(c->
{
if(c.getControlNewText().isEmpty())
{
返回c;
}
ParsePosition ParsePosition=新的ParsePosition(0);
Object Object=format.parse(c.getControlNewText(),parsePosition);
if(object==null | | parsePosition.getIndex()
{
doubleProperty.set(doubleProperty.get()+1.6);
} );
场景=新场景(新VBox(5));
舞台布景宽度(450);
舞台设置高度(250);
((VBox)scene.getRoot()).getChildren().addAll(字段,标签,b);
舞台场景;
stage.show();
}

感谢您给出这两个答案。我只能投一个票,所以我会投第一个答复的票,但是乌鲁克也给了我一些其他的想法。希望这些例子对其他人也有用。
String: null    Double: null
String: 3   Double: 3.14
String: 6   Double: 6.18
@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();
}