第一控制器中第二控制器的访问方法和标签

r1wp621o  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(282)

设计我有2个控制器类。第一个控制器是加载tableview和其他数据的主控制器类。有一个hbox,它在主控制器中加载第二个控制器(第二个控制器包含migpane、一行、一个按钮),它根据文件数据形成一个层次结构。第二个控制器可以根据文件数据多次添加到hbox区域。
目标我希望检索主控制器中第二个控制器的migpane的id。在构建层次结构时,每秒钟控制器的button事件都将在与migpane的id匹配之后填充tableview中的数据。
我已经查看了stackoverflow的很多帖子,但我仍然不明白我在哪里犯了错误。
主控制器

public class Controller implements Initializable
{
    ObservableList<SubSystem> s = FXCollections.observableArrayList();
    @FXML private TableView<SubSystem> SubsystemTable;
    @FXML private TableColumn<SubSystem, String> SubsystemParameters;
    @FXML private TableColumn<SubSystem, String> SubsystemDesc;
    @FXML private TableColumn<SubSystem, String> SubsystemValue;
    @FXML private TableColumn<SubSystem,String> SubsystemUnit;

    public void initialize(URL location, ResourceBundle resources) 
    {
        SubsystemParameters.setCellValueFactory(new PropertyValueFactory<SubSystem, String>("Parameter1"));
        SubsystemDesc.setCellValueFactory(new PropertyValueFactory<SubSystem, String>("Desc1"));
        SubsystemValue.setCellValueFactory(new PropertyValueFactory<SubSystem, String>("Value1"));
        SubsystemUnit.setCellValueFactory(new PropertyValueFactory<SubSystem, String>("Unit1"));
    }

    public void getSubsystemClicked() throws IOException
    {
        try
        {
            SubsystemTable.setItems(getSub());
        }
        catch (IOException e2)
        {
            e2.printStackTrace();
        }
    }

    public ObservableList<SubSystem> getSub() throws IOException
    {
        s.add(new SubSystem("A", "B", "C", "D"));
        return s;
    }

    public void addNodeButton() 
    {
        subsystem = new FXMLLoader(getClass().getResource("SubsystemNode.fxml"));
        try 
        {
           node = subsystem.load();
        } 
        catch (IOException e) 
        {
           e.printStackTrace();
        }
    }
}

第二控制器:

public class SubsystemNodeController implements Initializable
{
    @FXML public MigPane subsystemMigPane;
    @FXML public Button subsystem;
    @FXML public ImageView SubSystemImageView;
    private Controller MainController;

    public void initialize(URL url, ResourceBundle rb) 
    {
        subsystem.setOnAction(event-> setMigPane());
    }  

    public void setMigPane()
    {
        System.out.println("Subsystem Node Controller : " + subsystemMigPane.getId());
        System.out.println("Button ID generated is: " + subsystem.getId());
    }

    public String getMigPane()
    {
        System.out.println("Subsystem Node Controller called in Controller : " + subsystemMigPane.getId());
        return subsystemMigPane.getId();
    }
}

子系统表类

public class SubSystem 
{
    protected SimpleStringProperty parameter1;
    protected SimpleStringProperty desc1;
    protected SimpleStringProperty value1;
    protected SimpleStringProperty unit1;

    public SubSystem(String parameter, String desc, String value, String unit) 
    {
        super();
        this.parameter1 = new SimpleStringProperty(parameter);
        this.desc1 = new SimpleStringProperty(desc);
        this.value1 = new SimpleStringProperty(value) ;
        this.unit1 = new SimpleStringProperty(unit);
    }

    public String getParameter1() 
    {
        return parameter1.get();
    }
    public void setParameter1(String p)
    {
        this.parameter1 = new SimpleStringProperty(p);
    }

    public String getDesc1() 
    {
        return desc1.get();
    }
    public void setDesc1(String d)
    {
        this.desc1 = new SimpleStringProperty(d);
    }

    public String getValue1() 
    {
        return value1.get();
    }
    public void setValue1(String v)
    {
        this.value1 = new SimpleStringProperty(v);
    }

    public String getUnit1() 
    {
        return unit1.get();
    }
    public void setUnit1(String u)
    {
        this.unit1 = new SimpleStringProperty(u);
    }
}

注意,我尝试在主控制器中创建一个简单的时间函数,并将其传递给第二个控制器。它工作得很好,时间显示得很好。但是,当我试图调用主控制器中的第二个控制器来获取id时,它不起作用。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题