如何在关闭阶段之前从阶段返回值? [英] How to return value from a stage before closing it?

查看:31
本文介绍了如何在关闭阶段之前从阶段返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主舞台"我按下按钮打开第二阶段"在我有一张表格的地方,用户选择表格中的一项并点击asignar";按钮(它只是一个确认按钮),一旦点击,它必须将表中选择的项目的代码返回到主阶段并关闭第二阶段.

I have a "main stage" where I press a button to open a "second stage" where I have a table, the user selects one item of the the table and click on "asignar" button (which is just a confirm button), once clicked, it must return the code of the item selected in the table to the main stage and close the second stage.

这是重要的代码.

我有一个 INT 变量,它必须取函数的值:

I have an INT variable which must take the value of a function:

codigo = controller.setVista(this, usuario, password);

setVista"函数是这样的:

The "setVista" function goes like this:

public int setVista(ListHorarios vista, String usuario, String password) {
this.vista = vista;
this.usuario = usuario;
this.password = password;
this.inicializarTabla();
this.actualizarTabla(0, "%");
   
btnSeleccionar.setOnAction(e -> {
    asignarSeleccion();
    Stage stage = (Stage) btnSeleccionar.getScene().getWindow(); 
    stage.close();
});
    return codigo_horario;
}

和asignarSeleccion"像这样:

And the "asignarSeleccion" like this:

private void asignarSeleccion() {
    final HorarioTableModelo aux_horario = getTablaSeleccionada();
    posicion = datos.indexOf(aux_horario);
    if (aux_horario != null) {
        codigo_horario = aux_horario.getCodigo();
    }
}

我的问题是我无法获得codigo_horario"将值放入第一个变量codigo"中在舞台关闭之前,我错过了什么?

My problem is that I can't get the "codigo_horario" value into the first variable "codigo" before the stage closes, what do I am missing?

推荐答案

这是一个可能的例子.结构与在我的评论中的答案相同.

Here is a possible example. The structure is the same as in the answer in my comment.

第二个 Stage 通过一个控制器"打开,该控制器存储即使 Stage 关闭也应该返回的数据,并公开一个用于使用的 getter从外部世界检索值.

The second Stage is opened through a "controller" that is stores the data that should be returned even when the Stage is closed and exposes a getter to be used to retrieve the value from the outer world.

import javafx.application.Application;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);

            Button bSecondStage = new Button("Show second Stage");
            bSecondStage.setOnAction(e -> {
                WindowController wc = new WindowController();
                wc.showStage();
                System.out.println(wc.getData());
            });

            root.setCenter(bSecondStage);


            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

    class WindowController {
        private String data;

        void showStage() {
            Stage stage = new Stage();
            stage.initModality(Modality.APPLICATION_MODAL);

            VBox root = new VBox();
            Scene scene = new Scene(root);
            TextField tf = new TextField();
            Button submit = new Button("Submit");

            submit.setOnAction(e -> {
                data = tf.getText();
                stage.close();
            });

            root.getChildren().addAll(tf, submit);
            stage.setScene(scene);
            stage.showAndWait();
        }

        String getData() {
            return data;
        }
    }
}

这篇关于如何在关闭阶段之前从阶段返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆