Saturday, June 4, 2016

25. TreeView

A TreeView with 2 branches (area and pop) is created.


The branches have 10 items each. We have set everything to be expanded initially.


package ex25;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Ex25 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        
        primaryStage.setTitle("Example 25. TreeView");

        TreeItem root, pop, area;

        root = new TreeItem<>();
        root.setExpanded(true);

        pop = new TreeItem<>("Population");
        pop.setExpanded(true);
        area = new TreeItem<>("Area");
        area.setExpanded(true);
        root.getChildren().addAll(pop, area);
        
        pop.getChildren().addAll(   new TreeItem<>("China"),
                                    new TreeItem<>("India"),
                                    new TreeItem<>("United States"),
                                    new TreeItem<>("Indonesia"),
                                    new TreeItem<>("Brazil"),
                                    new TreeItem<>("Pakistan"),
                                    new TreeItem<>("Nigeria"),
                                    new TreeItem<>("Bangladesh"),
                                    new TreeItem<>("Russia"),
                                    new TreeItem<>("Japan"));
        
        area.getChildren().addAll(  new TreeItem<>("Russia"),
                                    new TreeItem<>("Canada"),
                                    new TreeItem<>("United States"),
                                    new TreeItem<>("China"),
                                    new TreeItem<>("Brazil"),
                                    new TreeItem<>("Australia"),
                                    new TreeItem<>("India"),
                                    new TreeItem<>("Argentina"),
                                    new TreeItem<>("Kazakhstan"),
                                    new TreeItem<>("Algeria"));
        
        TreeView  tree = new TreeView<>(root);
        tree.setShowRoot(false);
        
        StackPane layout = new StackPane();
        layout.getChildren().add(tree);
        Scene scene = new Scene(layout, 300, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

This is the output:


No comments:

Post a Comment