Bonjour,
je souhaite récupérer les variables du controller 1 dans la fonction initialize() du controller 2 pour l'injecter dans une requête sql.
J'aimerai avoir les démarches correctes.
Voici le code :

Main.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package application;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
 
public class Main extends Application {
 
	@Override
	public void start(Stage primaryStage) {
		try {
			 Parent root = FXMLLoader.load(getClass().getResource("/application/main.fxml"));
			 primaryStage.setTitle("main window");
			 //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			 primaryStage.setScene(new Scene(root));
			 primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}	
	public static void main(String[] args) {
		launch(args);
	}
}
mainController.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package application;
 
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
 
public class mainController implements Initializable{
 
	@FXML private AnchorPane mainfxml;		
	@FXML private TextField txtmain;
	@FXML private ComboBox<String> combobx;	
	@FXML private Button btnmain;
 
    ObservableList<String> list = FXCollections.observableArrayList("aa","bb","cc","dd");
    ArrayList<String> data = new ArrayList<String>(2);
 
	@Override
	public void initialize(java.net.URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
		combobx.setItems(list);
	}	
 
	public void btn_main(ActionEvent event) {
		try {	
			 FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/second.fxml"));
			 Parent root = loader.load();
			 data.add(combochanged().trim());
			 data.add(txtmain.getText().trim());
			 System.out.println(data.get(0));
			 System.out.println(data.get(1));			 
		     Stage stage = new Stage();
		     stage.setScene(new Scene(root));
		     stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}  				    		
	}
	public String combochanged() {       
		return combobx.getValue();
	}
}
secondController.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package application;
 
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
 
public class secondController implements Initializable{
 
	PreparedStatement prepareStatement0 = null;
	ResultSet resst0 = null;			
	Connection conn = null;	
	String query0 = null;	
 
	@FXML private AnchorPane second;
	@FXML private Label lblsecond;
 
    ArrayList<String> data1 = new ArrayList<String>(2);	
 
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
		 try {
			   Class.forName("com.mysql.cj.jdbc.Driver");		
			   String url = "jdbc:mysql://localhost:3306/test";
			   String username = "root";
			   String password = "";
 
			   Connection conn = DriverManager.getConnection(url,username,password);	
 
			   query0 = "SELECT var1,var2 FROM test_tab where var1=data1.get(0) and var2=data1.get(1)";
			   prepareStatement0 = conn.prepareStatement(query0);	
			   resst0 = prepareStatement0.executeQuery(); 
                   //--------------------
			      //reste du code
	           //-------------------- 
	           prepareStatement0.close();
	           resst0.close(); 			   
 
		    } catch (Exception e) {
				// TODO: handle exception
			    e.printStackTrace();
			} 			   			   
	}
}
Merci.