简体   繁体   English

经典对象和javafx对象

[英]classic objects and javafx objects

I'd like to ask one general question about JavaFX implementation strategy. 我想问一个关于JavaFX实施策略的一般性问题。 Let's say I already have a java project with many classes and objects that uses maybe swing as a GUI. 假设我已经有一个Java项目,其中包含许多类和对象,这些对象可能使用swing作为GUI。 Now I want to use JavaFX as GUI. 现在,我想使用JavaFX作为GUI。 I want to display those objects in nice JFX UI. 我想在漂亮的JFX UI中显示那些对象。 But to use JavaFX and its binding and all that stuff, classes need to utilize JavaFX style properties. 但是要使用JavaFX及其绑定和所有其他东西,类需要利用JavaFX样式属性。 The question is: what should I do with all those "old" classes and business objects I already have? 问题是:我应该如何处理已经拥有的所有那些“旧”类和业务对象? Do I need to rewrite at least those to be displayed to new JFX form? 我是否需要至少重写要显示为新JFX表单的内容? But those objects are also used for other purposes like database and file persistence. 但是这些对象还用于其他目的,例如数据库和文件持久性。 It doesn't seem to be OK to use JFX properties on plain business objects. 在普通业务对象上使用JFX属性似乎并不可行。 Should I wrap BOs in corresponding JFX "brothers"? 我应该将BO包裹在相应的JFX“兄弟”中吗? (This idea remainds me "ViewModel" from .NET WPF MVVM pattern.) Can somenone advice me what's the correct way how to deal with this? (这个想法使我无法从.NET WPF MVVM模式中获得“ ViewModel”。)有人可以建议我如何处理此问题的正确方法是什么? Thank you. 谢谢。

If you've got old classes that are inherently tied to Swing, then yes, to work with JavaFX those classes will need to be rewritten. 如果您有与Swing固有联系的旧类,那么可以,要使用JavaFX,这些类将需要重写。 JavaFX can work with listeners as well as binding directly - you can usually call x.propertyName().addListener() to add a change listener to that particular property, so there's no inherent need to bind things directly, and indeed sometimes an old fashioned listener is needed when binding won't suffice (though I do think it makes code neater when it can be done this way.) JavaFX可以与侦听器一起使用,也可以与直接绑定一起使用-您通常可以调用x.propertyName().addListener()将更改侦听器添加到该特定属性,因此不需要天生直接绑定事物,实际上有时不需要老式的绑定当绑定不足时就需要侦听器(尽管我确实认为可以通过这种方式使代码更整洁。)

The question is: what should I do with all those "old" classes and business objects I already have? 问题是:我应该如何处理已经拥有的所有那些“旧”类和业务对象? Do I need to rewrite at least those to be displayed to new JFX form? 我是否需要至少重写要显示为新JFX表单的内容? But those objects are also used for other purposes like database and file persistence. 但是这些对象还用于其他目的,例如数据库和文件持久性。 It doesn't seem to be OK to use JFX properties on plain business objects. 在普通业务对象上使用JFX属性似乎并不可行。

If those old classes and business objects don't use any Swing code, then you can just wrap them in FX wrappers as needed and provide all the properties you like (I'd use something like the decorator pattern for this.) There's nothing inherently wrong from a technical perspective of just providing JavaFX properties on these objects, but note that they're not Serializable and even if they were, I wouldn't consider this good practice. 如果那些旧类和业务对象不使用任何Swing代码,则可以根据需要将它们包装在FX包装器中,并提供所需的所有属性(为此,我将使用诸如装饰器模式之类的东西。)从仅在这些对象上提供JavaFX属性的技术角度来看是错误的,但是请注意,它们不是可序列化的,即使它们是可序列化的,我也不认为这是一种好习惯。 If they do use Swing code embedded in them then yes, this will all need to be swapped out - but this is a good example of why the UI code and application logic should be kept separate! 如果他们确实使用嵌入在其中的Swing代码,则需要全部替换掉​​-但这是为什么UI代码和应用程​​序逻辑应该分开的一个很好的例子!

I know the question is a bit old but just for the record: 我知道这个问题有点老了,但仅供参考:

Given you have a model class Person like this: 假设您有一个模型类Person如下所示:

public class Person {
    private String firstname;

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
}

You could write a wrapper class for JavaFX like this: 您可以这样编写JavaFX的包装器类:

public class PersonFx {

    private final StringProperty firstname = new SimpleStringProperty();

    private final Person person;

    public PersonFx(Person person) {
        this.person = person;
        firstname.setValue(person.getFirstname());

        firstname.addListener((observable, oldValue, newValue) ->
                person.setFirstname(newValue));
    }

    public String getFirstname() {
        return firstname.get();
    }

    public StringProperty firstnameProperty() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname.set(firstname);
    }
}

All changes to PersonFx will be reflected to the wrapped Person : PersonFx所有更改将反映到包装的Person

@Test
public void test(){
    Person person = new Person();

    PersonFx personFx = new PersonFx(person);


    personFx.setFirstname("luke");

    assertEquals("luke", personFx.getFirstname());
    assertEquals("luke", person.getFirstname());


    personFx.firstnameProperty().set("han");

    assertEquals("han", personFx.getFirstname());
    assertEquals("han", person.getFirstname());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM