2 Replies Latest reply: Mar 15, 2013 7:55 PM by shakir.gusaroff RSS

    Treeview and horizontal  scroll bars

    943794
      I have a treeview control and when I expand the nodes on the tree so that they exceed the width of the area allowed the treeview. I do not get a horizontal scroll bar, instead the labels for my nodes are truncated so that they fit within the width allowed for the treeview. How do I get the horizontal scroll bar to appear.
        • 1. Re: Treeview and horizontal  scroll bars
          993317
          You need to use a ScrollPane.

          ScrollPaneBuilder.create().content(treeView).pannable(true).build();

          Also check out the fitToWidth and fitToHeight methods of ScrollPane.
          • 2. Re: Treeview and horizontal  scroll bars
            shakir.gusaroff
            Hi. If you put the tree inside an HBox or VBox, horizontal scrollbar or vertical scrollbar are added automatically if the width or height exceed
            the area allowed for the tree, but it does not happen with all layouts. For example if a tree is inside a FlowPane layout container, the horizontal
            scrollbar is not added if the width exceeds the allowed area.

            If you add the following binds the horizontal scrollbar will be added.
            tree.prefWidthProperty().bind(root.widthProperty());
            tree.prefHeightProperty().bind(root.heightProperty());
            Here is an example.
              import java.util.Set;
            import javafx.application.Application;
            import javafx.event.ActionEvent;
            import javafx.event.Event;
            import javafx.event.EventHandler;
            import javafx.scene.Group;
            import javafx.scene.Node;
            import javafx.scene.Scene;
            import javafx.scene.control.TreeItem;
            import javafx.scene.control.TreeView;
            import javafx.scene.image.Image;
            import javafx.scene.image.ImageView;
            import javafx.scene.input.ScrollEvent;
            import javafx.scene.layout.BorderPane;
            import javafx.scene.layout.FlowPane;
            import javafx.scene.layout.HBox;
            import javafx.scene.layout.StackPane;
            import javafx.scene.layout.VBox;
            import javafx.stage.Stage;
             
            public class TreeS extends Application {
             
                 public static void main(String[] args) {
                    launch(args);
                }
                
                @Override
                public void start(Stage primaryStage) {
                    primaryStage.setTitle("Tree View Sample");        
                    
                    TreeItem<String> rootItem = new TreeItem<String> ("Inbox");
                    rootItem.setExpanded(false);
                    for (int i = 1; i < 106; i++) {
                        TreeItem<String> item = new TreeItem<String> ("Messageccccccccccccccccccccccccccccccccccccccccccccccccend" + i);            
                        rootItem.getChildren().add(item);
                    }        
                    TreeView<String> tree = new TreeView<String> (rootItem);        
                           
                    FlowPane root = new FlowPane();
                    root.getChildren().add(tree);
                    Scene scene = new Scene(root, 300, 250);
                    primaryStage.setScene(scene);
                    primaryStage.show();
                    
                     
                    tree.prefWidthProperty().bind(root.widthProperty());
                    tree.prefHeightProperty().bind(root.heightProperty());
                       
                }
            }