简体   繁体   中英

classic objects and javafx objects

I'd like to ask one general question about JavaFX implementation strategy. Let's say I already have a java project with many classes and objects that uses maybe swing as a GUI. Now I want to use JavaFX as GUI. I want to display those objects in nice JFX UI. But to use JavaFX and its binding and all that stuff, classes need to utilize JavaFX style properties. 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? 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. Should I wrap BOs in corresponding JFX "brothers"? (This idea remainds me "ViewModel" from .NET WPF MVVM pattern.) Can somenone advice me what's the correct way how to deal with this? 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. 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.)

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? 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.

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. 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!

I know the question is a bit old but just for the record:

Given you have a model class Person like this:

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:

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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