Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

root node (TreeItem) not showing in TreeView

I create TreeView in .fxml file and then i am trying to show root node. but it's not showing.

Here is my code.

<TabPane prefHeight="289.0" prefWidth="246.0" styleClass="tab-pane" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                  <stylesheets>
                    <URL value="@main.css" />
                  </stylesheets>
                  <tabs>
                    <Tab text="TestBed Explorer">
                      <content>
                        <AnchorPane id="Content" fx:id="soariteAnchorScollparent" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                          <children>
                            <ScrollPane fx:id="soariteTreeScrollPane" prefHeight="259.0" prefWidth="246.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                              <content>
                                <AnchorPane id="Content" fx:id="soariteTreeAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="249.0" prefWidth="73.0">
                                  <children>
                                    <TreeView fx:id="soariteTree" prefHeight="245.0" prefWidth="79.0" showRoot="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="167.0" AnchorPane.topAnchor="0.0">
                                      <TreeItem expanded="true" value="categories" fx:id="rootTreeItem" />
                                    </TreeView>
                                  </children>
                                </AnchorPane>
                              </content>
                            </ScrollPane>
                          </children>
                        </AnchorPane>
                      </content>
                    </Tab>
                  </tabs>
                </TabPane>      

I also give reference of this in main class like.

public class Mainextends Application {
@FXML
public TreeView<String> soariteTree;
@FXML
public TreeItem<String> rootTreeItem;

Please give me any reference or hint.

Upvotes: 0

Views: 1094

Answers (2)

Shreyas Dave
Shreyas Dave

Reputation: 3855

You have done a small mistake with fxml,

you can see you have written AnchorPane.rightAnchor="167.0" which is making your tree view disappear (and same small mistakes with width of anchor pane and tree view).

replace your of scroll pane with,

<ScrollPane fx:id="soariteTreeScrollPane" prefHeight="259.0" prefWidth="246.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                              <content>
                                <AnchorPane id="Content" fx:id="soariteTreeAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="-1.0" prefWidth="-1.0">
                                  <children>
                                    <TreeView fx:id="soariteTree" prefHeight="-1.0" prefWidth="-1.0" showRoot="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                      <TreeItem expanded="true" value="categories" fx:id="rootTreeItem" />
                                    </TreeView>
                                  </children>
                                </AnchorPane>
                              </content>
                            </ScrollPane>

UPDATE :- Handling of Mouse Event

soariteTree.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (event.getButton().equals(MouseButton.SECONDARY)) {
                System.out.println(">> " + event.getEventType());
            }
        }
    });

Upvotes: 5

Shreyas Dave
Shreyas Dave

Reputation: 3855

your fxml will be,

<TreeView fx:id="categoryTreeView" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minWidth="200.0" prefHeight="-1.0" prefWidth="200.0" showRoot="true" styleClass="master-tree" VBox.vgrow="ALWAYS">
          <TreeItem expanded="true" value="categories" fx:id="categoryTreeItem" />
</TreeView>

And Your Controller will be,

public class MyClass extends Application {
@FXML private TreeItem<ItemMaster> categoryTreeItem;
@FXML private TreeView<ItemMaster> categoryTreeView;

So you dont need to create a root for your tree. And you are done.

Upvotes: 2

Related Questions