很久以前就问过这个问题:)无论如何,最近我一直在寻找解决方案并以这种方式实现-单例Form类实现了抽象类:
public abstract class InputForm {
private ArrayList<BindingPair> bindedProps;
private ArrayList<ListenerPair> listenerPairs;
public InputForm() {
bindedProps = new ArrayList();
listenerPairs = new ArrayList();
}
public void resetBindings() {
unbindAll();
bindedProps = new ArrayList();
}
public void resetListeners() {
removeAllListeners();
listenerPairs = new ArrayList();
}
private void unbindAll() {
for (BindingPair pair : bindedProps) {
unbind(pair);
}
}
private void removeAllListeners() {
for (ListenerPair listenerPair : listenerPairs) {
removeListener(listenerPair);
}
}
public void bind(BindingPair bindingPair) {
BindingUtils.bind(bindingPair);
bindedProps.add(bindingPair);
}
public void addListener(ListenerPair listenerPair) {
ListenerUtils.addListener(listenerPair);
listenerPairs.add(listenerPair);
}
private void unbind(BindingPair bindingPair) {
BindingUtils.unbind(bindingPair);
}
private void removeListener(ListenerPair listenerPair) {
ListenerUtils.removeListener(listenerPair);
}
}
public class BindingUtils {
public static void bind(BindingPair bindingPair) {
if (bindingPair.isBidirectionalBinding()) {
if (bindingPair.getStringConverter() != null)
Bindings.bindBidirectional(bindingPair.propertyToBindProperty(), bindingPair.propertyBindToProperty(), bindingPair.getStringConverter());
else
bindingPair.propertyToBindProperty().bindBidirectional(bindingPair.propertyBindToProperty());
} else {
bindingPair.propertyToBindProperty().bind(bindingPair.propertyBindToProperty());
}
}
public static void unbind(BindingPair bindingPair) {
if (bindingPair.isBidirectionalBinding()) {
bindingPair.propertyToBindProperty().unbindBidirectional(bindingPair.propertyBindToProperty());
} else {
bindingPair.propertyToBindProperty().unbind();
}
}
}
public class ListenerUtils {
public static void addListener(ListenerPair listenerPair) {
if (listenerPair.propertyProperty() != null)
listenerPair.propertyProperty().addListener(listenerPair.getListener());
if (listenerPair.roPropertyProperty() != null)
listenerPair.roPropertyProperty().addListener(listenerPair.getListener());
}
public static void removeListener(ListenerPair listenerPair) {
if (listenerPair.propertyProperty() != null)
listenerPair.propertyProperty().removeListener(listenerPair.getListener());
if (listenerPair.roPropertyProperty() != null)
listenerPair.roPropertyProperty().removeListener(listenerPair.getListener());
}
}
public class BindingPair {
private Property propertyToBind;
private Property propertyBindTo;
private boolean bidirectionalBinding;
private StringConverter stringConverter;
public BindingPair(Property propertyToBind, Property propertyBindTo, boolean bidirectionalBinding, StringConverter stringConverter) {
this.propertyToBind = propertyToBind;
this.propertyBindTo = propertyBindTo;
this.bidirectionalBinding = bidirectionalBinding;
this.stringConverter = stringConverter;
}
public Property propertyToBindProperty() {
return propertyToBind;
}
public Property propertyBindToProperty() {
return propertyBindTo;
}
public boolean isBidirectionalBinding() {
return bidirectionalBinding;
}
public StringConverter getStringConverter() {
return stringConverter;
}
}
public class ListenerPair {
private Property property = null;
private ChangeListener listener;
private ReadOnlyObjectProperty roProperty = null;
public ListenerPair(Property property, ChangeListener listener) {
this.property = property;
this.listener = listener;
}
public ListenerPair(ChangeListener listener, ReadOnlyObjectProperty roProperty) {
this.listener = listener;
this.roProperty = roProperty;
}
public Property propertyProperty() {
return property;
}
public ChangeListener getListener() {
return listener;
}
public Object getRoProperty() {
return roProperty.get();
}
public ReadOnlyObjectProperty roPropertyProperty() {
return roProperty;
}
}
在 Form 类(实现 InputForm)中,每次通过此表单“渲染”新对象时都会调用两个方法:
private void doBinding() {
resetBindings();
bind(new BindingPair(dataXlsFileUrlProp, getControllerMain().getSceneRenderer().getLoadedSceneInfo().xlsDataFileProperty(), false, null));
}
private void addListeners() {
resetListeners();
addListener(new ListenerPair(dataXlsFileUrlProp, (observable, oldValue, newValue) -> {
//do smth
}));
}
希望有人会帮助这个。