Selecting a TreeItem will display a different scene

49 views Asked by At

I'm creating a stage that displays a TreeTable that has the following TreeItems: Photos, Files and Videos. When a user selects on one of the items I want the scene to change and display either the photos, files or videos.

Here is a snippet of the TreeTable:

enter image description here

I understand how to get the value of the tree item and display.

public void selectedItem(){
    TreeItem<String> item = dataTable.getSelectionModel().getSelectedItem();
    if(item != null){
        System.out.println(item.getValue());
    } else if (item == pngFiles) {
        System.out.println("You've selected PNG Files");
    }        
}

I have also tried to manipulate this code block to have the item variable equal to one of the tree items defined. That didn't work appropriately.

1

There are 1 answers

1
SedJ601 On BEST ANSWER

Here is something that may be helpful. You may need tree.getSelectionModel().selectedItemProperty().addListener.

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class App extends Application {

    private final Node rootIcon = new ImageView(
        //new Image(getClass().getResourceAsStream("folder_16.png"))
    );

    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Tree View Sample");        
        
        TreeItem<String> rootItem = new TreeItem ("Inbox", rootIcon);
        rootItem.setExpanded(true);
        for (int i = 1; i < 6; i++) {
            TreeItem<String> item = new TreeItem ("Message" + i);            
            rootItem.getChildren().add(item);
        }        
        TreeView<String> tree = new TreeView (rootItem);   
        
        BorderPane root = new BorderPane();
        root.setCenter(getViewOne());
        root.setLeft(tree);
        
        tree.getSelectionModel().selectedItemProperty().addListener((obs, oldTreeItem, newTreeItem) -> {
            if(newTreeItem != null)
            {
                if(newTreeItem == rootItem.getChildren().get(0))
                {
                    root.setCenter(getViewOne());
                }
                if(newTreeItem == rootItem.getChildren().get(1))
                {
                    root.setCenter(getViewTwo());
                }
                else
                {
                    System.out.println("Write code for other cases!");
                }
            }
        });
        
        primaryStage.setScene(new Scene(root, 500, 500));
        primaryStage.show();
    }
    
    
    public HBox getViewOne()
    {
        return new HBox(new Label("View One"));
    }
    
    public StackPane getViewTwo()
    {
        return new StackPane(new Label("View Two"));
    }
}

Edit: You can also do something like the following.

tree.getSelectionModel().selectedItemProperty().addListener((obs, oldTreeItem, newTreeItem) -> {
    if(newTreeItem != null)
    {
        if(newTreeItem.getValue().equals("Message1"))
        {
            root.setCenter(getViewOne());
        }
        if(newTreeItem.getValue().equals("Message2"))
        {
            root.setCenter(getViewTwo());
        }
        else
        {
            System.out.println("Write code for other cases!");
        }
    }
});

enter image description here

Altered code from https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm.