0

Design I have 2 controller classes. The first controller is the main controller class that loads a tableview and other data. There is an HBox which loads the second controller in the main controller (the second controller contains a MigPane, a line, a Button) that forms a hierarchy structure depending on the file data. The second controller can be added in the HBox area multiple times depending on the file data.

Goal I wish to retrieve the ID of the MigPane of the 2nd controllers in the main controller. As I am building a hierarchy structure, each second controller's button event will fill data in the tableview after matching with the ID of the MigPane.

I have looked into many posts of StackOverFlow and I still don't understand where I am making the mistake.

Main Controller

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

Second Controller:

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

Subsystem Table class

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

Note I have tried creating a simple time function in the main controller and passing it to the second controller. It is working well and displaying the time properly. But when I try to call the second controller in the main controller to get the ID, it doesn't work.

kleopatra
  • 50,242
  • 28
  • 96
  • 201
  • 1
    Start [here](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx). If you master this concept, it will solve your problem. – SedJ601 Jan 09 '21 at 04:50
  • 1
    you have to pass the controller instance/s around as parameters somewhere, for options see https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml Unrelated: stick to java naming conventions please! – kleopatra Jan 09 '21 at 04:53

0 Answers0