Javafx:在按钮上单击以打开第二个选择窗口

时间:2019-05-19 19:36:08

标签: button javafx window

我正在制作一个按钮,用于打开一个新窗口,其中包含成员的网格视图,其中显示了成员的姓名和等级。可以单击一个成员将其设置为currentMember。但是我的JavaFX知识有限,而且我不知道如何在不更改当前控制器的情况下创建此临时窗口。我不确定我所拥有的是否是一个好方法。我是在做错什么,还是这样做的正确方法?

单击“选择人员”按钮时,将运行此代码

@FXML
    private void setLid(ActionEvent event) {
        Stage stage = new Stage();
        VBox box = new VBox();
        box.setPadding(new Insets(10));

        TableView<Persoon> tablePers = new TableView<>();
        TableColumn<Persoon, String> voornaam = new TableColumn<>();
        TableColumn<Persoon, String> achternaam = new TableColumn<>();
        TableColumn<Persoon, String> graad = new TableColumn<>();

        voornaam.setCellValueFactory(cellData -> cellData.getValue().voornaamProperty());
        voornaam.setCellFactory(TextFieldTableCell.forTableColumn());
        achternaam.setCellValueFactory(cellData -> cellData.getValue().achternaamProperty());
        graad.setCellValueFactory(cellData -> cellData.getValue().graadProperty());

        Label label = new Label("Selecteer een persoon");

        Button btnSelectCurrentLid = new Button();
        btnSelectCurrentLid.setText("Bevestigen");

        btnSelectCurrentLid.setOnAction((ActionEvent e) -> {
            geselecteerdePersoon = (tablePers.getSelectionModel().getSelectedItem());
            stage.close();
        });

        box.getChildren().add(label);
        box.getChildren().add(tablePers);
        box.getChildren().add(btnSelectCurrentLid);

        Scene scene = new Scene(box, 250, 150);
        stage.setScene(scene);
        stage.show();
    }

1 个答案:

答案 0 :(得分:0)

这里是一个文件mcve(将整个代码复制粘贴到FxMain.java中并运行):

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FxMain extends Application {

    private Persoon geselecteerdePersoon;

    @Override
    public void start(Stage primaryStage) throws Exception{

        Label selectedPersonInfo = new Label();
        Button start = new Button("Show Tabel");
        start.setOnAction(e-> {
            new LoginDialog().showAndWait();
            selectedPersonInfo.setText("Selected: " +geselecteerdePersoon.voornaamProperty().get()
                                          + " " +geselecteerdePersoon.achternaamProperty().get());
        });

        BorderPane root = new BorderPane(selectedPersonInfo);
        root.setBottom(start);
        primaryStage.setScene(new Scene(root, 300,300));
        primaryStage.show();
    }

    public static void main(final String[] args) {
        launch(args);
    }

    class LoginDialog extends Dialog {

        public LoginDialog() {

            VBox box = new VBox();
            box.setPadding(new Insets(10));

            TableView<Persoon> tablePers = new TableView<>();
            TableColumn<Persoon, String> voornaam = new TableColumn<>("Name");
            TableColumn<Persoon, String> achternaam = new TableColumn<>("Last Name");
            TableColumn<Persoon, String> graad = new TableColumn<>("Grade");

            voornaam.setCellValueFactory(cellData -> cellData.getValue().voornaamProperty());
            voornaam.setCellFactory(TextFieldTableCell.forTableColumn());
            achternaam.setCellValueFactory(cellData -> cellData.getValue().achternaamProperty());
            graad.setCellValueFactory(cellData -> cellData.getValue().graadProperty());

            tablePers.getColumns().addAll(voornaam, achternaam, graad);
            tablePers.getItems().add(new Persoon("Alice", "Bee","70"));
            tablePers.getItems().add(new Persoon("Charly", "Din","32"));

            Label label = new Label("Selecteer een persoon");

            Button btnSelectCurrentLid = new Button();
            btnSelectCurrentLid.setText("Bevestigen");

            btnSelectCurrentLid.setOnAction((ActionEvent e) -> {
                 geselecteerdePersoon = tablePers.getSelectionModel().getSelectedItem();
                 close();
            });

            box.getChildren().addAll(label, tablePers, btnSelectCurrentLid);
            getDialogPane().setContent(box);
            getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL);
        }
    }
}

class Persoon {

    private final SimpleStringProperty lName;
    private final SimpleStringProperty fName;
    private final SimpleStringProperty grade;

    public Persoon(String fName, String lName, String grade) {

        this.fName = new SimpleStringProperty(fName);
        this.lName = new SimpleStringProperty(lName);
        this.grade = new SimpleStringProperty(grade);
    }

    public final StringProperty achternaamProperty() {  return lName;   }

    public final StringProperty voornaamProperty() { return fName; }

    public final StringProperty graadProperty() { return grade;  }
}