如何使用Scene Builder将HashMap传递给JavaFX控制器?

时间:2018-12-01 14:08:15

标签: java javafx scenebuilder

我是JavaFX和Scene Builder的新手。我构建了一个Java程序来注册车辆及其加油。在我的主班我有这个:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        primaryStage.setTitle("Controle de Abastecimentos");
        primaryStage.setScene(new Scene(root, 700, 400));
        primaryStage.show();

        Cadastro cadastro = new Cadastro();

        cadastro.novoVeiculo("AAA-1555", "Volkswagen", "Polo", 2018, 55);
        cadastro.novoVeiculo("BAC-2555", "Fiat", "Palio", 2012, 40);
        cadastro.novoVeiculo("AAD-4555", "Mitsubishi", "Outlander", 2013, 65);
        cadastro.novoVeiculo("DAA-1555", "Kia", "Soul", 2011, 50);
        cadastro.novoVeiculo("ADA-0555", "Porsche", "Cayenne", 2015, 70);

        cadastro.veiculos.get("AAA-1555").addAbastecimento("Comum", "01/05/2018", 0, 50, new BigDecimal("4.29"));
        cadastro.veiculos.get("AAA-1555").addAbastecimento("Comum", "05/05/2018", 300, 50, new BigDecimal("4.45"));
        cadastro.veiculos.get("AAA-1555").addAbastecimento("Comum", "15/05/2018", 750, 50, new BigDecimal("4.35"));
        cadastro.veiculos.get("AAA-1555").addAbastecimento("Comum", "25/10/2018", 800, 20, new BigDecimal("4.29"));

    }

    public static void main(String[] args) throws IOException {

        launch(args);
    }
}

它基本上实例化一个Cadastro对象,该对象创建一个HashMap并将Veiculo存储在其中:

public class Cadastro {
    public Map<String, Veiculo> veiculos = new HashMap<>();

    public Cadastro() {}

    public void novoVeiculo (String placa, String fabricante, String modelo, int ano, double capacidadeTanque) {
        if (veiculos.containsKey(placa)) {
            throw new IllegalArgumentException("Veículo já cadastrado");
        }
        veiculos.put(placa, new Veiculo(placa, fabricante, modelo, ano, capacidadeTanque));
    }
}

我的问题是使用JavaFX和Scene Builder。在Main类的控制器中,我做了一个函数来调用TableView所在的Scene:

public class Controller {

    public void loadPesquisarVeiculos(ActionEvent actionEvent) throws IOException {
        Stage main = (Stage) ((Node)actionEvent.getSource()).getScene().getWindow();
        Pane pesquisaVeiculo = FXMLLoader.load(getClass().getResource("carInformation.fxml"));
        main.setScene(new Scene(pesquisaVeiculo));
    }

}

因此,当我调用loadPesquisarVeiculos时,我的场景将变为具有新Controller的TableView所在的场景。

public class VeiculoTableController implements Initializable {

    @FXML private TableView<Veiculo> tableView;
    @FXML private TableColumn<Veiculo, String> placa;
    @FXML private TableColumn<Veiculo, String> fabricante;
    @FXML private TableColumn<Veiculo, String> modelo;
    @FXML private TableColumn<Veiculo, Integer> ano;
    @FXML private TableColumn<Veiculo, Double> capacidadeTanque;
    @FXML private TableColumn<Veiculo, Double> odometro;
    ObservableList<Veiculo> veiculos = FXCollections.observableArrayList();

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

        placa.setCellValueFactory(new PropertyValueFactory<Veiculo, String>("placa"));
        fabricante.setCellValueFactory(new PropertyValueFactory<Veiculo, String>("fabricante"));
        modelo.setCellValueFactory(new PropertyValueFactory<Veiculo, String>("modelo"));
        ano.setCellValueFactory(new PropertyValueFactory<Veiculo, Integer>("ano"));
        capacidadeTanque.setCellValueFactory(new PropertyValueFactory<Veiculo, Double>("capacidadeTanque"));
        odometro.setCellValueFactory(new PropertyValueFactory<Veiculo, Double>("odometro"));
        tableView.setItems(veiculos);

    }

}

我认为我必须将cadastro传递给ObservableList<Veiculo> veiculos = FXCollections.observableArrayList(I think I should put the cadastro HashMap here);,但是我不知道如何将cadastro传递给第一个控制器,然后传递给第二个控制器,因为使用Scene Builder他们没有构造函数,而是使用FXML文件。

任何帮助将不胜感激。我已经阅读了很多教程,但是它们都显示了如何创建TableView来实例化控制器类中的对象,这不是一个选择。

0 个答案:

没有答案