JavaFX Scala File-TreeView: read out 2 levels

179 Views Asked by At

I want to implement a file browser in scala with a GUI made with JavaFx. Therefore i use a TreeView to represent the Tree.

This code only reads out one level from the tree whenever i click on to one item (performance reasons) but the treeview does not show indents and the arrows at the non-empthy folders are missing.

If i read out 2 levels (in order to check if a folder is empty or not), it only shows me the files without directories.

Has anyone of you guys an idea how that could work?

Thanks in advance!

  val rootPath: String = "C:/Test/"
  val path1 = Paths.get(rootPath)
  val opts = util.EnumSet.of(FileVisitOption.FOLLOW_LINKS)

  def treeRecursive(path: Path): Unit = {

      Files.walkFileTree(path,opts, 1, new SimpleFileVisitor[Path]() {

        override def visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult = {


          if (attrs.isDirectory()){
            val subdir = new TreeItem[String](path.toString, new ImageView(pictureFolder))
            root.getChildren.add(subdir)
          }
          else{
            val file = new TreeItem[String](path.toString, new ImageView(pictureFile))
            root.getChildren.add(file)
          }

          FileVisitResult.CONTINUE
        }

        override def visitFileFailed(path: Path, exc: IOException): FileVisitResult = {
          println(path + " failed")
          FileVisitResult.CONTINUE;
        }

      } )
    }


  val mouseEvent: EventHandler[_ >: MouseEvent] = new EventHandler[MouseEvent] {
    override def handle(event: MouseEvent): Unit = {
      event.getSource match {
        case clicked: TreeView[String] => treeRecursive(Paths.get(clicked.getSelectionModel.getSelectedItem.getValue))
      }
    }
  }

Here is a Screenshot from my current treeview! Screenshot

and it should look like this screenshot Screenshot2

0

There are 0 best solutions below