0

My goal is to load a TreeView component which shows folders containing passwords. The actual folders and passwords within the folders will be pulled from a database eventually, but before I do that, I wanted to do a dry run to make sure the TreeView component would actually display.

This is my first attempt at using a TreeView component; the rest of the window loads properly, but the TreeView itself does not display. I've watched some youtube videos and browsed all the related Stack Overflow posts relating to TreeView and from what I can tell I've followed the tutorials correctly. Clearly, I am doing something wrong, so I was hoping someone could take a look at my code and perhaps help determine what I'm doing wrong (or recommend a better implementation). For reference, the tutorial I was following is here.

The "main" window itself is loaded from another Controller class when the user clicks a login button. The login screen uses one FXML file while the main window uses a separate FXML file which should load the TreeView.

Main Controller:

public class MainController implements Initializable
{
    @FXML
    private BorderPane borderPane;
    @FXML
    private MenuBar menuBar;
    @FXML
    private TreeView<String> treeView;

    //helper method to populate the main nodes
    public ArrayList<TreeItem<String>> getFolders()
    {
        ArrayList<TreeItem<String>> folders = new ArrayList<>();
        //populate the weblogin parent node
        TreeItem<String> webLogins = new TreeItem<>("Web Logins");
        webLogins.getChildren().addAll(getWebLogins());
        //populate the printerLogins parent node
        TreeItem<String> printerLogins = new TreeItem<>("Printer Logins");
        printerLogins.getChildren().addAll(getPrinterPasswords());

        folders.add(webLogins);
        folders.add(printerLogins);

        return folders;
    }
    //create the children nodes related to web logins
    private ArrayList<TreeItem<String>> getWebLogins()
    {
        ArrayList<TreeItem<String>> webLogins = new ArrayList<>();

        TreeItem<String> google = new TreeItem<>("google");
        TreeItem<String> apple = new TreeItem<>("apple");
        TreeItem<String> facebook = new TreeItem<>("facebook");

        webLogins.add(google);
        webLogins.add(apple);
        webLogins.add(facebook);

        return webLogins;
    }
    //create the children nodes related to printer passwords
    private ArrayList<TreeItem<String>> getPrinterPasswords()
    {
        ArrayList<TreeItem<String>> printerPass = new ArrayList<TreeItem<String>>();

        TreeItem<String> konica = new TreeItem<>("Konica Minolta");
        TreeItem<String> ricoh = new TreeItem<>("Ricoh");
        TreeItem<String> hp = new TreeItem<>("Hewlett Packard");

        printerPass.add(konica);
        printerPass.add(ricoh);
        printerPass.add(hp);

        return printerPass;
    }


    @Override
    public void initialize(URL location, ResourceBundle resources)
    {
        ArrayList<TreeItem<String>> allFolders = getFolders();
        treeView = new TreeView<>();
        TreeItem<String> rootNode = new TreeItem<>("Folder Types");
        rootNode.getChildren().addAll(allFolders);
        this.treeView.setRoot(rootNode);
    }
}

Main Window FXML File:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.passwordmanager.gui.controllers.MainController">
   <top>
      <MenuBar fx:id="menuBar" BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <left>
      <TreeView fx:id="treeView" prefHeight="372.0" prefWidth="263.0" BorderPane.alignment="CENTER" />
   </left>
</BorderPane>

Thanks in advance to anyone who takes a look at this and tries to help me figure out what my mistake is.

tushar_lokare
  • 461
  • 1
  • 8
  • 22
FuegoJohnson
  • 372
  • 6
  • 18
  • Even though the linked question asks about `TableView`, the issue/solution are basically the same. You need to get rid of `treeView = new TreeView<>();`, since that value is filled by `FXMLLoader` with the `TreeView` created based on the fxml automatically. – fabian Mar 16 '19 at 10:01
  • @fabian It worked thank you for linking the solution. I guess I didn't find it because I was searching for TreeView things specifically. – FuegoJohnson Mar 16 '19 at 19:23

0 Answers0