如何从另一个控制器在组合框中加载数据

时间:2018-10-24 18:49:07

标签: java javafx

您好,我在ComboBox中有一个MainController声明,我想在ComboBox中传递来自该ConexionController的所有数据

cargavistauno中的方法ConexionController

我正在研究中发现了这样的东西

public void cargavistauno(){
            try {
                FXMLLoader fxmlLoader = new FXMLLoader();
                fxmlLoader.setLocation(getClass().getResource("CargaDatosGenero.fxml"));
                Scene scene = new Scene(fxmlLoader.load());
                Main Controller = fxmlLoader.<Main>getController();
                Controller.llenadocombobox();
               fxmlLoader.setController(Controller);
                Stage stage = new Stage();
                stage.setTitle("Datos Del Cliente");
                stage.setScene(scene);
                stage.show();
            } catch (IOException e) {

            }

        }

我在Maincontroller中有此方法,也有ComboBox

 @FXML private ComboBox <String> combo;

@Override
public void start(Stage stage) {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("Vista.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        fxmlLoader.setController(combo);
        stage.setTitle("Project Xcorpio");
        stage.setScene(scene);
        stage.show();
    } catch (IOException e) {

    }

     }

public void llenadocombobox() {
        Connection conn=null;
        try {
            ObservableList<String> listacombo= FXCollections.observableArrayList();
            String consulta = "select genero from super";
            conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-TUP82C7:1433;databaseName=prueba", "sa", "milkas87");
            PreparedStatement ps =conn.prepareStatement(consulta);
             ResultSet rs = ps.executeQuery();

             while ( rs.next() ) 
             {  
               listacombo.add(rs.getString("genero"));


             }

          combo.setItems(listacombo);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

如何传递这些值?任何需要的方向或帮助。谢谢。

0 个答案:

没有答案