本文介绍了JavaFX:将TextProperty(例如Label)绑定到一个简单的Integer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般问题:当简单整数的值发生变化时,有没有办法更新标签?

The general question: Is there any way to update a label when the value of a simple integer changes ?

我说的是简单的int而不是像。我根据
(我必须将ObservableValue的标识符(就是所谓的名称?)从Integer更改为String,因为我无法找到将其绑定到TextProperty的方法,否则)

I'm talking about simple int's and not stuff like ReadOnlyIntegerWrappers. I've tried the following according to Converting Integer to ObservableValue<Integer> in javafx(I had to change the identifier (is that what it's called ?) of ObservableValue from Integer to String because I couldn't find a way to bind it to the TextProperty otherwise)

我在下面包含了我的演示代码,它似乎在 label.textProperty()。bind(m.getObsValue());似乎导致了NullPointerException。 。该应用程序以MVC模式编写。

I've included my demo code below which somehow seems to result in a NullPointerException at label.textProperty().bind(m.getObsValue());. The application is written in a MVC-pattern.

模型:

public class Model {

private int value;
private ObservableValue<String> obsInt;

public Model(){
    value = 5;
    obsInt = new ReadOnlyObjectWrapper<>(value + "");
}

public int getValue(){
    return value;
}

public void setValue(int value){
    this.value = value;
}

public ObservableValue<String> getObsValue(){
    return obsInt;
}
}

控制器:

public class Controller {
private Model m;
private View v;

public Controller(Model m, View v){
    this.m = m;
    this.v = v;
}

public void handleMouseclick(MouseEvent e){
    m.setValue(m.getValue() + 5);
}
public void init(){
    v.setOnMouseClicked(this::handleMouseclick);
}
}

查看:

public class View extends Region{

private Model m;
private Label label;

public View(Model m)
{
    this.m = m;

    label.textProperty().bind(m.getObsValue());
    label.setLayoutX(200);
    label.setLayoutY(200);
    paint();
}

public void paint(){
    getChildren().clear();        
    getChildren().addAll(label);  
}


@Override
public double computePrefHeight(double width){
    return 800;
}

@Override
public double computePrefWidth(double height){
    return 600;
}
}

你可能已经注意到我现在还在学习JavaFX。所以我可能只是错过了一些愚蠢的东西。任何建议都将不胜感激!

As you might've noticed I'm currently still studying JavaFX. So I probably just missed something stupid. Any advice would be greatly appreciated !

推荐答案

让我们从结束开始 - 例外是因为你永远不会初始化 label ,所以它为null - 就这么简单。使用 label = new Label(); 应该解决它。

Let's start from the end - the exception is because you never initialize label, so it is null - as simple as that. Using label = new Label(); should solve it.

现在对于绑定 - 你说你不想使用 IntegerProperty ReadOnlyIntegerWrapper ,而是使用简单的 int - 这意味着你没有方便的方法来知道值何时被改变!标签将始终包含整数的初始值,因此您可以执行以下操作:

And now for the bindings - you say you don't want to use IntegerProperty or ReadOnlyIntegerWrapper, but rather use a simple int - that means you have no convenient way of knowing when the value is changed! The label will always contain the initial value of your integer, so you may as well do something like:

label.setText(Integer.toString(m.getValue()));

相反,我会建议你做点什么

Instead, I would advise you to do something like

public class Model {

    private SimpleIntegerProperty value = new SimpleIntegerProperty(this, "value");

    public Model() {
        value.set(5);
    }

    public int getValue(){
        return value.get();
    }

    public void setValue(int value){
        this.value.set(value);
    }

    public IntegerProperty valueProperty(){
        return value;
    }
}

然后你可以使用<$绑定标签的文本属性c $ c> Bindings.convert :

label.textProperty().bind(Bindings.convert(m.valueProperty()));

这样,只要模型的值发生变化,标签文本就会自动反映出来。

this way, whenever the model's value is changed, the label text would automatically reflect this.

如您所见, SimpleIntegerProperty 无所畏惧!构造函数中的参数是可选的,但建议 - 它们是此属性所属的对象( this ),以及属性的名称(,在这种情况下)。您也可以在构造函数中传递初始值,而不是在 Model 构造函数中显式设置它。

As you can see, SimpleIntegerProperty is nothing to be afraid of! The arguments in the constructor are optional, but recommended - they are the object this property belongs to (this), and the name of the property ("value", in this case). You can also pass the initial value in the constructor, instead of explicitly setting it in your Model constructor.

这篇关于JavaFX:将TextProperty(例如Label)绑定到一个简单的Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 22:50