Bind JavaFX TextField to a String value from two SimpleStringValues


Question

I have three JavaFX TextFields, one for name, one for domain, and the final for the FQDN (Fully Qualified Domain Name).

I have a class that contains SimpleStringProperties for name, domain, and FQDN. I am have not been able to create a binding for FQDN that takes the values set in SimpleStringProperty name and SimpleStringProperty domain and creates SimpleStringProperty name@domain. I was trying to build the strings by combining the name and domain and adding @ however I cannot get it back to a SimpleStringProperty to send to the GUI for FQDN.

I've included a rough example of what I've done. I have the GUI and many other components working, I just cannot get the FQDN to report correctly.

I did this previously in Java Swing by adding an ActionListener that would listen for mouse movement and then update the field. I am trying to improve on this.

public class Data {
static SimpleStringProperty name = new SimpleStringProperty();
static SimpleStringProperty domain = new SimpleStringProperty();
static SimpleStringProperty FQDN = new SimpleStringProperty();

public static void setName(String string) {
    name.set(string);
}

public static void setDomain(String string) {
    domain.set(string);
}

public static ObservableStringValue getFQDN() {
    FQDN.set(name.get() + "@" + domain.get());
    return FQDN;
}

}

public class GUI {
TextField name = new TextField();
TextField domain = new TextField();
TextField FQDN = new TextField();

name.textProperty().addListener(new NameChange());
domain.textProperty().addListener(new DomainChange());
FQDN.textProperty().bind(Data.getFQDN());
}

public class NameChange implements ChangeListener<String> {

@Override
public void changed(ObservableValue<? extends String> observable, 
                                                          String oldValue, 
                                                          String newValue) {
    Data.setName(newValue);
}

}

public class DomainChange implements ChangeListener<String> {

@Override
public void changed(ObservableValue<? extends String> observable,
                                                     String oldValue, 
                                                     String newValue) {
    Data.setDomain(newValue);
}

}

1
3
6/9/2014 6:19:10 PM

Accepted Answer

Can't really see why you would ever make everything in the Data class static, however:

public class Data {
static StringProperty name = new SimpleStringProperty();
static StringProperty domain = new SimpleStringProperty();
static StringProperty FQDN = new SimpleStringProperty();

static {
    FQDN.bind(Bindings.format("%s@%s", name, domain));
}

public static String setName(String string) {
    name.set(string);
}

public static String setDomain(String string) {
    domain.set(string);
}

public static ObservableStringValue getFQDN() {
    return FQDN;
}
}
6
6/9/2014 6:03:50 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon