使用Scene Builder使用其他类中实例化的对象创建JavaFX TableView

时间:2018-11-30 19:41:47

标签: java javafx tableview

我正在学习Java,并且试图使用Scene Builder在JavaFX中生成一个表。我创建了一个课程Cadastro

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));
    }
}

在我的Main类中,我禁用了cadastro并将新的Veiculo对象添加到HashMap:

    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();
    }

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

        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);

        launch(args);
    }

}

现在,我试图显示一个Veiculo的表,但是我看过的所有在线教程都显示了如何创建使该控制器类中的对象实例化的表,如下所示:

    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(new Veiculo Objects);

    @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);

    }
}

因此,如果我将ObservableList中的对象实例化,则可以创建表。

我在上一个场景的控制器中调用TableView使用此功能的场景:

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));
}

我的问题是,如何将在VeiculoTabController类中实例化的HashMap传递给Main类?我是Scene Builder的新手,我不确定它是如何实例化控制器的,所以我不确定如何将HashMap传递给控制器​​。预先感谢

0 个答案:

没有答案