在事件发生后更新 javafx 中文本字段上的值



我想在事件发生后更新JavaFx textfield上的值。我有一个打开popupRootScreen,在那个popup我有一个listview.当用户在该listview上选择项目时,它应该更新RootScreentexfield上的值。 这是每个列表项上存在的button操作的代码。每个列表项还有一个TEXTFIELDbutton来选择它。我希望TEXTFIELD上的值位于RootScreentexfield上。请参阅大写和小写,因为我试图使其尽可能易于理解。

public void initialize() {
button.setOnAction(event -> {
source = select.getParent();
//walletname is the name i want on textfield.
walletName = textField.getText();
getWalletName(walletName);
Stage stage = (Stage) source.getScene().getWindow();
stage.close();


});
}
private void getWalletName(String walletName){
profilePopup.SetText(walletName,rootScreenController);
}

SetText方法在Popup View Class中。SetText方法的代码。

public void SetText(String walletName, OnClick onClick){
onClick.onMouseClicked(walletName);
}

我有一个interface OnClick,它有一个onMouseClicked方法。我在RootScreen中实现的接口

public interface OnClick {
void onMouseClicked(String name);

}

这就是我在界面中覆盖方法的方式。

@Override
public void onMouseClicked(String walletName) {
textfield.setText(walletname);
}

但它不会更新文本字段上的值。 我是Java的新手,所以我不确定在这里做什么。

如果您有一个弹出屏幕来填充 ListView 中的信息,我认为最好是 ListView 条目是对象或集合中的对象。这是分隔 ListView 上每个条目的唯一方法 - 从对象中提取信息,而不是从 ListView 派生单个项。

这是一个可运行的示例,我认为您过于复杂,您可以使用.setOnMouseClicked功能来简化此操作

public class Main extends Application {
public static void main(String[] args) { launch(args); }
@Override
public void start(Stage primaryStage) {
TextField textField = new TextField("Not the same");
ListView<String> listView = new ListView<>();
listView.getItems().add("Item 1");
listView.getItems().add("Item 2");
listView.getItems().add("Item 3");
listView.setOnMouseClicked(event -> {//You can change this if need be
textField.setText(listView.getSelectionModel().getSelectedItem());
});
VBox vBox = new VBox();
vBox.getChildren().addAll(listView, textField);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
}

有关ListView的更多信息,请查看此内容,您可能还想在此处查看对象属性

最新更新