JavaFX отправка данных из одного FXML в другой - PullRequest
0 голосов
/ 24 апреля 2019

У меня есть простая программа, над которой я работаю. Это в основном похоже на трекер задач, у него есть главное представление, которое состоит из представления таблицы и меню добавления, другого этапа. Как я могу получить данные из AddingController и отправить их в «ViewController», чтобы информация отображалась в моем табличном представлении. Я пытался использовать облист, но, похоже, он не работает или я что-то делаю неправильно.

Спасибо,

ViewController:

package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class ViewController implements Initializable {



    ContextMenu context = new ContextMenu();

    @FXML
    TableView<ModelTable> tableView;

    @FXML
    public TableColumn<String, ModelTable> columnId;
    @FXML
    public TableColumn<String, ModelTable> columnDesc;
    @FXML
    public TableColumn<String, ModelTable> columnCat;
    @FXML
    public TableColumn<String, ModelTable> columnResp;

    ObservableList<ModelTable> oblist = FXCollections.observableArrayList();





    @Override
    public void initialize(URL location, ResourceBundle resources) {
        MenuItem itemOpen = new MenuItem("Open");
        MenuItem itemAdd = new MenuItem("Add");
        MenuItem itemRemove = new MenuItem("Remove");

        context.getItems().addAll(itemOpen, itemAdd, itemRemove);

        tableView.setContextMenu(context);

        itemAdd.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                try {
                    Parent addTaskParent = FXMLLoader.load(getClass().getResource("AddingMenu.fxml"));
                    Scene addTaskScene = new Scene(addTaskParent);
                    Stage stage = new Stage();
                    stage.setTitle("Adding Ticket");
                    stage.setScene(addTaskScene);
                    stage.show();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        });


    columnId.setCellValueFactory(new PropertyValueFactory<>("id"));
    columnDesc.setCellValueFactory(new PropertyValueFactory<>("description"));
    columnCat.setCellValueFactory(new PropertyValueFactory<>("category"));
    columnResp.setCellValueFactory(new PropertyValueFactory<>("responsible"));




    tableView.setItems(oblist);

    }
}

Добавление контроллера

package sample;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

import java.net.URL;
import java.util.ResourceBundle;


public class AddingController implements Initializable {

    @FXML
    Button btnSubmit;
    @FXML
    Button btnCancel;
    @FXML
    Label lblReqID;
    @FXML
    ChoiceBox cbCategory;
    @FXML
    ChoiceBox cbResponsible;
    @FXML
    TextArea taDesc;

    ObservableList<ModelTable> oblist = FXCollections.observableArrayList();


    public void submit(ActionEvent e) {

        if (cbCategory.getValue() != null && cbResponsible.getValue() != null && taDesc.getLength() > 3) {

            oblist.add(new ModelTable(123, taDesc.getText(), cbCategory.getValue().toString(),cbResponsible.getValue().toString()));

        } else {
            System.out.println("Booooooo!!!");
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        cbCategory.getItems().add("Support");
        cbCategory.getItems().add("Maintenance");
        cbResponsible.getItems().add("R");
        cbResponsible.getItems().add("T");

    }

}

Таблица моделей:

package sample;

public class ModelTable {
    int id;
    String Description, Category, Responsible;

    public ModelTable(int id, String description, String category, String responsible) {
        this.id = id;
        Description = description;
        Category = category;
        Responsible = responsible;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getCategory() {
        return Category;
    }

    public void setCategory(String category) {
        Category = category;
    }

    public String getResponsible() {
        return Responsible;
    }

    public void setResponsible(String responsible) {
        Responsible = responsible;
    }
}
...