为什么JavaBean适配器不更新JavaFX属性

为什么JavaBean适配器不更新JavaFX属性

本文介绍了为什么JavaBean适配器不更新JavaFX属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX的新手,需要GUI(使用JavaFX属性)和旧的Java代码(使用JavaFX属性)之间的双向绑定。我尝试使用JavaBean适配器,如下面的简单示例所示:

I'm new to JavaFX and need a bidirectional binding between a GUI (using JavaFX properties) and my old Java code (not using JavaFX properties). I tried using a JavaBean adapter as the following simple example shows:

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.adapter.JavaBeanDoubleProperty;
import javafx.beans.property.adapter.JavaBeanDoublePropertyBuilder;

public class App
{
    public static void main(String[] args) throws NoSuchMethodException
    {
        Pojo pojo = new Pojo();
        FXModel model = new FXModel();

        JavaBeanDoubleProperty adapter = JavaBeanDoublePropertyBuilder
                .create().bean(pojo).name("value").build();

        model.valueProperty().bindBidirectional(adapter);

        System.out.println("Adapter before: " + adapter.get());
        System.out.println("Model before: " + model.getValue());
        System.out.println("Bean before: " + pojo.getValue());

        pojo.setValue(123d);

        System.out.println();
        System.out.println("Adapter after: " + adapter.get());
        System.out.println("Model after: " + model.getValue());
        System.out.println("Bean after: " + pojo.getValue());
    }

    public static class Pojo
    {
        private double value;

        public double getValue()
        {
            return value;
        }

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

    public static class FXModel
    {
        private final DoubleProperty value = new SimpleDoubleProperty();

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

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

        public DoubleProperty valueProperty()
        {
            return value;
        }
    }
}

生成的输出是

适配器之后: 123.0模型之后:0.0 Bean之后:123.0

Adapter after: 123.0 Model after: 0.0 Bean after: 123.0

因此,通过将普通Java对象设置为新值,将通知JavaBeanDoubleProperty关于更改,但JavaFX属性不是,虽然它绑定到适配器。为什么?

So, by setting the "normal" Java object to a new value the JavaBeanDoubleProperty is informed about the change, but the JavaFX property is not, although it is bound to the adapter. Why?

即使向Pojo添加PropertyChangeSupport,如上所述,

Even by adding a PropertyChangeSupport to Pojo as described here,

public static class Pojo
    {
        private double value;
        private PropertyChangeSupport pcs;

        public Pojo()
        {
            pcs = new PropertyChangeSupport(this);
        }

        public double getValue()
        {
            return value;
        }

        public void setValue(double value)
        {
            final double oldValue = this.value;
            this.value = value;
            pcs.firePropertyChange("name", oldValue, value);
        }

        public void addPropertyChangeListener(
                final PropertyChangeListener listener)
        {
            pcs.addPropertyChangeListener(listener);
        }
    },

它不起作用。 (不幸的是,使用也没有用。)

it does not work. (Unfortunately, a second trial using BeanPathAdapter also did not work.)

推荐答案

你刚刚在 Pojo中做了一个单据。 setValue(double value)。此方法为属性name触发PropertyChangeEvent。它应该是价值。
只需将其更改为

You just made a slip in Pojo.setValue(double value). This method fires a PropertyChangeEvent for the property "name". It supposed to be "value".Just change it to

public void setValue(double value)
{
    final double oldValue = this.value;
    this.value = value;
    pcs.firePropertyChange("value", oldValue, value);
}

它会起作用。

这篇关于为什么JavaBean适配器不更新JavaFX属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:00