Exchange information between Javafx windows

Asked

Viewed 641 times

1

Good afternoon, I need to exchange information between different windows (Scene) with javafx, I searched the net and saw that the most common is to use controller communication. My scenario is this: Home screen the user will select a state in a list and when clicking on search, another screen will open with only the cities of that state. I would like a simple example of how to exchange this information. Grateful.

2 answers

0

Hello, if you can add dependencies to your project I suggest the lib Windowcontrollerfx (link here), it facilitates the initialization and creation of Stages (windows) and passing parameters. Using the library would look something like this:

Search class:

public class FrmLisaCidades extends WindowControllerFx{

    @Override
    public void getFXML(){
      return "/view/tela_busca.fxml";
    }

   private String estado;

   public FrmLisaCidades(String estado){
      super();
      this.estado = estado;
   }

   @Override
   public void initialize(URL location, ResourceBundle resources) {

      List<Cidade> lista = new cidadeDao().getListaByEstado(estado);
      // Demais funções
   }
}

Using the library you don’t have to worry about Fxmlloader and Stages, plus you can show the screen directly by the controller:]

new FrmListaCidades().show();

Can you do without the lib? Yeah, but it’s a little more boring:

ListaCidadesController controller = meuFxmlLoader.getController();
controller.setEstado(meuEstado); // esse método deve ser criado no controller

0

I don’t have the example, but if you post the code of what you did I might be able to help you with what you want.

I imagine that you are using FXML, a way that perhaps is simpler is instead of using Fxmlloader you could instantiate the control class normally and then inject the control in FMXL. So you could use the class constructor to receive any object you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.