将操作设置为在按钮上执行一次即可执行两项操作 [英] Set Action on button to do two action in one click
问题描述
这里是场景,我有一个主窗口,然后单击一个按钮,它会打开一个弹出窗口.在这个弹出窗口中,我有一个表视图,其中显示了一些数据,并且它有一个名为选择"的按钮.从表格视图中选择数据后,因此当我按下选择按钮时,我希望关闭此弹出窗口,并从中选择的数据显示在主窗口中.
Here is the scenario, I have a main window and I click on one button it opens a pop up window. In this pop window I have table view that have some data display in it, and it have a one button called select. After select the data from table view, so when I push the select button I want this pop window to close and the data I selected from that to appear in my main window.
到目前为止,我唯一能做的就是从弹出窗口中提取数据,我希望它只需单击一下即可关闭
So far only thing I can do is extract the data from pop up window, I want it to close aswell with just one click
private void venueDisplay(String title, String message) {
Stage window = new Stage();
//Block events to other windows
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(400);
HBox hBox = new HBox();
hBox.setPadding(new Insets(10,10,10,10));
hBox.setSpacing(10);
hBox.setMaxHeight(20);
hBox.setAlignment(Pos.BOTTOM_CENTER);
hBox.getChildren().add(selectVenueButton);
//Display all the available venues to choose for allocation
VBox layout = new VBox(10);
venueList = new ListView<>();
ObservableList<Venue> observableVenue = FXCollections.observableArrayList(model.getVenues());
venueList.setItems(observableVenue);
layout.getChildren().addAll(venueList, hBox);
//Display window and wait for it to be closed before returning
Scene scene1 = new Scene(layout,300,500);
window.setScene(scene1);
window.showAndWait();
}
public void selectButtonHandler(EventHandler<ActionEvent> handler) {
selectVenueButton.setOnAction(handler);
}
推荐答案
我认为您可以做到:
private Venue venueDisplay(String title, String message) {
// existing code..
window.showAndWait();
return venueList.getSelectionModel().getSelectedItem();
}
,然后您的selectVenueButton
只需要关闭窗口:
and then your selectVenueButton
just needs to close the window:
selectVenueButton.setOnAction(e -> window.hide());
这篇关于将操作设置为在按钮上执行一次即可执行两项操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!