【问题标题】:how to share data between two stages in javafx如何在javafx中的两个阶段之间共享数据
【发布时间】:2015-03-12 21:15:24
【问题描述】:

我有两个fxml窗口登录和主窗口。它们各自的控制器如下:

public class MainwindowController extends Stage implements Initializable {

    @FXML private Button Send;
    @FXML private TextField txtBcast;
    @FXML private ListView listviewUsers;
    @FXML  Label lblDisplayName;


    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */ 

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        ObservableList<String> chat =FXCollections.observableArrayList ("default");
        listviewUsers.setItems(chat);

    }

  public void  setLblName(String msg){

         lblDisplayName.setText(msg);
          }


    @FXML public void ActionSend(ActionEvent e){
        send();
        txtBcast.setText("");
    }

    private void send() {
    if (txtBcast.getText().isEmpty())
        return;   
  //  chatManager.sendPublicMsg(format,txtBcast.getText());
    }

    /**
     *
     * @param e
     * @throws Exception
     */
    @FXML public void ActionUserSelected( MouseEvent e) throws Exception{
                  //  String lineRest = e.getActionCommand();
       if(e.getClickCount()==2)
       {
           if(!listviewUsers.getSelectionModel().isEmpty())
           {

            String str=(String)listviewUsers.getSelectionModel().getSelectedItem(); 


                Parent main= FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Usertab.fxml"));
                Scene scene = new Scene(main);
                Stage stage = new Stage();
                stage.setTitle(str);
                stage.setScene(scene);
                stage.show();

            }

           else { JOptionPane.showMessageDialog(null, "Oops! it seems you are trying to click the list view"); }

       }
       //Stage pstage = (Stage)listUsers.getScene().getWindow();
       //pstage.close();  
    }         
}

还有

public class LoginwindowController extends Stage implements Initializable {


    @FXML private LoginwindowController loginwindowController;
    @FXML private MainwindowController mainwindowController;
    @FXML private Button btnSignIn; 
    @FXML private TextField txtDisplayName;
    @FXML private ToggleGroup Gender;
    @FXML private ComboBox comboStatus;
    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */

    @Override 
    public void initialize(URL url, ResourceBundle rb) {
        ObservableList<String> items =FXCollections.observableArrayList ("Online","Offline");
        comboStatus.setItems(items);
        writeToTextField();



    }

    public void writeToTextField() {

        String username = System.getProperty("user.name");
        txtDisplayName.setText(""+ username);
    }



    @FXML protected void ActionSignIn(ActionEvent event) throws Exception {

        mainwindowController.setLblName(txtDisplayName.getText());

       InetAddress addr = InetAddress.getLocalHost();

        if(addr.isLoopbackAddress())
        {
            Dialogs.create().message("Oops! It seems you are not connected to any network..\n :(").showError();
        }
        else{ 

              start(txtDisplayName.getText());// start chat manager

              Parent root= FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Mainwindow.fxml"));
              Scene scene = new Scene(root);
              Stage stage = new Stage();
              stage.setTitle("LetsChat-Welcome "+ txtDisplayName.getText());
           // Context.getInstance().setDisplayName(txtDisplayName.getText());
              stage.setScene(scene);
              stage.getIcons().add(new Image("/letschat/images/logo.png"));
               Stage pstage = (Stage)btnSignIn.getScene().getWindow(); 
               stage.show();
               pstage.close();

        }


    } 

    private void start(String name) {
        try {
            ChatManager ic = new ChatManager(name);
            ic.start();
                    } catch (Exception ex) {
                            Dialogs.create().message( "Could not start the chat session\nCheck that there no other instances running :(").showError();
                            }
    }
}

当用户单击signin 按钮时,我希望主窗口中的标签lblDisplayName 使用登录窗口中的txt 显示名称中的文本进行更新。有人可以帮助如何做到这一点......请尽快

【问题讨论】:

  • 应用从这里开始
  • public class LetsChat extends Application { public void start(Stage primaryStage)throws Exception { Parent root=FXMLLoader.load(getClass().getResource("/letschat/fxwindows/Loginwindow.fxml"));场景场景 = 新场景(根); primaryStage.getIcons().add(new Image("/letschat/images/logo.png")); primaryStage.setTitle("LetsChat - 登录"); primaryStage.setScene(场景); primaryStage.setResizable(false);主舞台.show(); } public static void main(String[] args) { 启动(args);}}

标签: javafx


【解决方案1】:

有多种方法可以做到这一点,在您的情况下,登录将创建另一个阶段,因此一种简单的方法是创建一个新的 FXML 加载器(变量名:myLoader),如果您想将用户的用户名传递为构造函数参数,您可以使用 myLoader.setControllerFactory 并作为返回:

return clazz == MyController.class ? new MyController(userName) : null;

MyController 是要读取用户名的 Controller 的名称

如果你想使用 set 方法,你可以使用 getController 获取控制器实例并调用 set 方法(例如,myController.setUsername());

创建自定义 FXML

FXMLLoader myLoader = new FXMLLoader(<if you use relative paths, here you should pass the position);

记得调用 load(),因为 URI 重载是静态的。 (即,使用 getResourceAsStream)。

如果您的应用程序又大又复杂,您可以使用 EventBus(我在任何地方都喜欢它..)

【讨论】:

    【解决方案2】:

    我不确定我是否完全理解两个控制器之间的关系,以及与控制器对应的 FXML 文件,但看起来 LoginWindowController 加载了 MainWindow.fxml,我猜是 @ 987654323@ 是MainWindow.fxml 的控制器。

    在这种情况下,你可以这样做

       @FXML protected void ActionSignIn(ActionEvent event) throws Exception {
    
    
           InetAddress addr = InetAddress.getLocalHost();
    
            if(addr.isLoopbackAddress())
            {
                Dialogs.create().message("Oops! It seems you are not connected to any network..\n :(").showError();
            }
            else{ 
    
                  start(txtDisplayName.getText());// start chat manager
    
                  FXMLLoader loader = new FXMLLoader(getClass().getResource("/letschat/fxwindows/Mainwindow.fxml"));
                  Parent root= loader.load();
                  MainWindowController mainWindowController = loader.getController();
                  mainWindowController.setLblName(txtDisplayName.getText());
    
                  Scene scene = new Scene(root);
                  Stage stage = new Stage();
                  stage.setTitle("LetsChat-Welcome "+ txtDisplayName.getText());
               // Context.getInstance().setDisplayName(txtDisplayName.getText());
                  stage.setScene(scene);
                  stage.getIcons().add(new Image("/letschat/images/logo.png"));
                   Stage pstage = (Stage)btnSignIn.getScene().getWindow(); 
                   stage.show();
                   pstage.close();
    
            }
    
    
        } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多