從您的SecondController
公開StringProperty
。當按下按鈕,將其值設置:
public class SecondController {
private final StringProperty selectedValue = new SimpleStringProperty(this, "selectedValue", "");
public final StringProperty selectedValueProperty() {
return selectedValue ;
}
public final void setSelectedValue(String value) {
selectedValue.set(value);
}
public final String getSelectedValue() {
return selectedValue.get();
}
@FXML
private final ComboBox<String> comboBox ;
@FXML
private void handleButtonPress() {
selectedValue.set(comboBox.getValue());
}
}
在你FirstController
,用於設置文本的方法:
public class FirstController {
@FXML
private TextField textField ;
public void setText(String text) {
textField.setText(text);
}
}
現在,當您加載FXML文件,只是觀察的屬性SecondController
,並在FirstController
更改時調用該方法:
FXMLLoader firstLoader = new FXMLLoader(getClass().getResource("first.fxml"));
Parent first = firstLoader.load();
FirstController firstController = firstLoader.getController();
FXMLLoader secondLoader = new FXMLLoader(getClass().getResource("second.fxml"));
Parent second = secondLoader.load();
SecondController secondController = secondLoader.getController();
secondController.selectedValueProperty().addListener((obs, oldValue, newValue) ->
firstController.setText(newValue));
不同的FXML文件是如何加載的?其中一個是'',還是都是從一些常見的位置加載的? –
2014-11-01 23:56:24
這兩個fxml文件都是從一些常見的loacation中加載的 – user3414063 2014-11-03 07:12:26