JavaFX将数据从一种FXML发送到另一种

时间:2019-04-24 11:55:20

标签: java javafx javafx-8

我正在开发一个简单的程序。它基本上就像一个任务跟踪器,它的主视图由一个表视图和一个添加菜单组成,一个不同的阶段。我如何从AddingController中获取数据并将其发送到“ ViewController”,以便信息出现在我的表视图中。我曾尝试使用oblist,但似乎不起作用,或者我做错了。

谢谢

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

0 个答案:

没有答案