这是一个场景,我有一个主窗口,我点击一个按钮就会打开一个弹出窗口。在这个弹出窗口中,我有一个表视图,其中显示了一些数据,它有一个名为select的按钮。从表格视图中选择数据后,当我按下选择按钮时,我希望关闭此弹出窗口,并从中选择的数据显示在我的主窗口中。
到目前为止,我唯一能做的就是从弹出窗口中提取数据,我希望它只需点击一下即可关闭
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);
}
答案 0 :(得分:2)
请考虑这个示例,您可以接受这个想法并将其应用到您的程序中(注释中的说明)。
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class GetInfoFromPopUpWindow extends Application{
static TextArea textArea = new TextArea(); // to be filled from the pop-up window
@Override
public void start(Stage primaryStage) throws Exception {
// create the main Window and some simple components
// Suppose it contains a TextArea only for simplicity sake
Button open = new Button("Open Popup Window");
//simple container as a root for testing
HBox root = new HBox();
root.getChildren().addAll(textArea, open);
Scene scene = new Scene(root,610,400);
primaryStage.setScene(scene);
primaryStage.setTitle("Main Window");
primaryStage.show();
//Add Action Listener to the open Button
open.setOnAction(e->{ // lambda expression, read more about it in the Documentation
popUpWindow(); // call the method to open a pop-up wondow(see later)
});
}
public static void popUpWindow(){
VBox root = new VBox();
Button fetchInfo = new Button("Finish");
//create a listView and populate it with some info for testing purpose
// suppose the info you get from some database
ListView<String> listView = new ListView<String>();
ObservableList<String> items = FXCollections.observableArrayList (
"First Item", "Second Item", "Third Item", "Fourth Item");
listView.setItems(items);
//to select more than one item
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
//decoration and size are up to your preference
listView.setPrefWidth(100);
listView.setPrefHeight(100);
root.getChildren().addAll(listView, fetchInfo);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 250,150);
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);
stage.setTitle("Popup Window");
stage.show();
// Add action listener to fetchInfo Button in this Window
fetchInfo.setOnAction(e->{
// take the info from listView and fill it in the TextArea in the main Window
// just for testing purposes
for (String selectedItem : listView.getSelectionModel().getSelectedItems()){
textArea.appendText(selectedItem + " \n");
}
// when it finishes -> close the window and back to the first one
stage.close();
});
}
public static void main(String[] args) {
launch();
}
}
<强>测试强>
点击任意按钮之前的主窗口
点击按钮并选择某些项目后的弹出窗口
点击完成按钮后,它关闭弹出窗口,然后返回主菜单,显示信息(所选项目)
答案 1 :(得分:2)
我认为你可以做到:
private Venue venueDisplay(String title, String message) {
// existing code..
window.showAndWait();
return venueList.getSelectionModel().getSelectedItem();
}
然后你的selectVenueButton
只需要关闭窗口:
selectVenueButton.setOnAction(e -> window.hide());
答案 2 :(得分:1)
您想要点击选择按钮
执行两项操作要实现此设置按钮上的事件处理程序,如下所示
selectVenueButton.setOnAction(handler);
在处理程序中,您可以编写逻辑来关闭弹出窗口,如下所示:
private EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Object source = event.getSource();
if (source instanceof Button) {
Button btn = (Button) source;
Stage stage = (Stage) btn.getScene().getWindow();
stage.close();
}
}
};
要在类级别范围(成员变量)上实现此声明场地列表,因此您可以在课堂外访问。 在Dialog类中:
ListView<Venue> venueList;
访问主窗口中的数据:
CustomDialog dialog = new CustomDialog(); //popup class
dialog.showDialog;
Venue selectedItem = dialog.venueList.getSelectionModel().getSelectedItems();