I have three domain objects in my app as follows :
public class Workflow {
private String name;
private List<Sheet> sheets;
}
public class Sheet {
private String name;
private List<Task> tasks;
}
public class Task {
private String name;
}
All three are dependent as Workflow -> Sheet -> Task. My goal is to build TreeView so that it look like below :
-Workflow
|
- workflow name
-Sheets
|
- sheet name
- Tasks
|
- task name
So far, I have build a sample that builds more less what I expect, but it's not generic and 'automated' at all.
public class TreeViewSample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Tree View Sample");
Workflow w = setup();
TreeItem<String> rootItem = new TreeItem<String> ("Workflow");
rootItem.setExpanded(true);
TreeItem<String> item = new TreeItem<String> (w.getName());
rootItem.getChildren().add(item);
(...)
TreeView<String> tree = new TreeView<String> (rootItem);
StackPane root = new StackPane();
root.getChildren().add(tree);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private Workflow setup(){
Workflow wflow = new Workflow();
wflow.setName("wflow name");
wflow.setSheets(Arrays.asList(new Sheet("sheet name", Arrays.asList(new Task("task name")))));
return wflow;
}
Can someone advise on how I can recursively go to my domain objects and build the TreeView as in my example ?
You have to create a common
Modelto all of you models(Workflow, Sheet,Task), since all have a String property, it is quite simple to create one. Let's suppose we have the following model:I kept there together all of your models, to see them better.
I see you don't use any
.fxmlfile at your app, mine is with.fxmlI recommend that you separate at least to separate theMainclass from theControllerclass, like:Then the Controller class:
Just in case if you need here is the
.fxmlfile:If you don't know the depth of the
TreeViewyou can create all of the branches or leaves using recursion. In this case it is much simpler to use two foreachs instead of creating a recursive method which builds the tree structure.