0

I have a HierarchyTabController which I call this method once on initial load of the app.

public void displayRootTreeStructure()
{
    //for loops through each Requirement to display all RequirementGroups
}

The problem is, I have another controller called RequirementGroupsController which has a method the user can then add with a click of a button.

  @FXML
  private void addRequirementGroup()
  {
  }

So, in other words, if the user adds a Requirement Group and then switches to the Hierarchy Tab, it is out of date. How can I call the displayRootTreeStructure method when the user calls addRequirementGroup?

lakeIn231
  • 1,177
  • 3
  • 14
  • 34
  • It sounds like you need a model class, and need to share an instance of that class with each controller. Then the controllers can observe the model and update the UI as required when the data changes. (This is the essence of MVC-like design patterns.) Have a look at http://stackoverflow.com/questions/32342864/applying-mvc-with-javafx for an example of the pattern. – James_D Apr 24 '17 at 14:49
  • The problem is, only one controller is modifying the data. The other controller is just displaying the data. – lakeIn231 Apr 24 '17 at 15:30
  • Exactly. Share a model instance between the two controllers. The controller displaying the data can observe the data in the model and update the UI as needed. The controller that modifies the data will modify those data, and then the other controller will automatically update it. The model for a tree might be more complex that the simple model in the example I linked, but the structural ideas are identical. – James_D Apr 24 '17 at 15:33

1 Answers1

0

Presuming the two methods are in separate classes (you didn't specify):

Pass a reference of the displayRootTreeStructure class to the addRequirementGroup class.

DisplayRootTreeStructureClass ref = YOUR_REFERENCE_TO_THE_OBJECT;
@FXML
  private void addRequirementGroup()
  {
     ref.displayRootTreeStructure();
  }

Alternatively, you can make displayRootTreeStructure static and access it from the class directly. However, as James_D has stated, be aware of the complications that may arise when taking this route.

public class DisplayRootTreeStructureClass{

  public static void displayRootTreeStructure(){
     //do stuff
  }
}

and you would access it by calling from the class name directly, no object needed:

@FXML
  private void addRequirementGroup()
  {
     DisplayRootTreeStructureClass.displayRootTreeStructure();
  }

You should try give more details with your questions. It makes it much easier to answer your question fully. As of writing this, I don't even know if this is the right answer because you didn't give enough detail.

DCON
  • 628
  • 5
  • 18
  • Don't make the method static. What if you wanted two such trees displayed in the same application? (What if your boss asks you to add that functionality in 6 months: if you have made the method static you will have to rewrite potentially large chunks of the application in order to accommodate the new functionality.) – James_D Apr 24 '17 at 15:17
  • "Be aware of complications...". Ah, just don't do it. The `static` keyword is about scope, and the decision about whether a method should be static or not should be made according to what the appropriate scope is. This method is modifying something that is a property of the specific *instance* of this class: the controller object that is associated with a specific view. It is clearly not a property of the class as a whole, but a property of the controller instance. Making this method `static` is plain wrong. – James_D Apr 24 '17 at 15:35