JavaFX TreeView 🌳

Поділитися
Вставка
  • Опубліковано 8 лют 2025
  • javafx treeview treeitem tutorial example explained
    #javafx #treeview #treeitem

КОМЕНТАРІ • 22

  • @BroCodez
    @BroCodez  3 роки тому +15

    package application;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.TreeItem;
    import javafx.scene.control.TreeView;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.input.MouseEvent;
    public class Controller implements Initializable{

    @FXML
    private TreeView treeView;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

    TreeItem rootItem = new TreeItem("Files");
    //TreeItem rootItem = new TreeItem("Files", new ImageView(new Image("Folder_Icon.png")));

    TreeItem branchItem1 = new TreeItem("Pictures");
    TreeItem branchItem2 = new TreeItem("Videos");
    TreeItem branchItem3 = new TreeItem("Music");

    TreeItem leafItem1 = new TreeItem("picture1");
    TreeItem leafItem2 = new TreeItem("picture2");
    TreeItem leafItem3 = new TreeItem("video1");
    TreeItem leafItem4 = new TreeItem("video2");
    TreeItem leafItem5 = new TreeItem("music1");
    TreeItem leafItem6 = new TreeItem("music2");

    branchItem1.getChildren().addAll(leafItem1, leafItem2);
    branchItem2.getChildren().addAll(leafItem3, leafItem4);
    branchItem3.getChildren().addAll(leafItem5, leafItem6);

    rootItem.getChildren().addAll(branchItem1, branchItem2, branchItem3);

    //treeView.setShowRoot(false);
    treeView.setRoot(rootItem);
    }

    public void selectItem() {

    TreeItem item = treeView.getSelectionModel().getSelectedItem();

    if(item != null) {
    System.out.println(item.getValue());
    }
    }
    }

    • @ЙирюВонаирда
      @ЙирюВонаирда 3 роки тому

      Hello, Bro.
      Wanna ask you about this FX stuff and all. Currently is there any sense in using swing? Or FX is the better and bestiest and all?
      Didnt find a video about trees in swing-playlist and so want to ask this.

    • @rapbimsfrienda22
      @rapbimsfrienda22 3 роки тому

      Thanks for your great Explanation ☺️

    • @mcebabds
      @mcebabds 7 місяців тому

      I continuously ran in to problems, when enter the last line of code --> adding image to the rootItem,
      In the end I could only resolve it by adding a few lines.
      @Override
      public void initialize(URL arg0, ResourceBundle arg1) {
      //, new ImageView(new Image("Folder_Icon.png")) Import as it gave an error in the import method

      Image folderIcon = new Image(getClass().getResourceAsStream("Folder_Icon.png"));
      // Resize the image to fit within reasonable dimensions --> image was way to big)
      ImageView iconView = new ImageView(folderIcon);
      iconView.setFitWidth(16); // Set the desired width
      iconView.setFitHeight(16); // Set the desired height

      TreeItem rootItem = new TreeItem("Files", iconView);

  • @RipperRoo92
    @RipperRoo92 3 роки тому +8

    I'm lucky that I have found this channel.
    It's like finding a golden egg in a easter egg hunt.

  • @techwithgantavya6381
    @techwithgantavya6381 3 роки тому +2

    Yo Bro Thanks A Lot I learnt Java Just Because Of You

  • @sethg7336
    @sethg7336 3 роки тому +1

    Thank you bro you are the best

  • @medinyonlineschool4493
    @medinyonlineschool4493 3 роки тому

    Thanks to share

  • @erikalcalasalero3810
    @erikalcalasalero3810 3 роки тому

    Amazing video ! is there a chance you can explain how to drag and drop treeview items wthin the treeview ? All the best !

  • @ЙирюВонаирда
    @ЙирюВонаирда 3 роки тому

    Hello, Bro.
    Wanna ask you about this FX stuff and all. Currently is there any sense in using swing? Or FX is the better and bestiest and all?
    Didnt find a video about trees in swing-playlist and so want to ask this.

    • @ЙирюВонаирда
      @ЙирюВонаирда 3 роки тому

      *for the desktop applications
      I still want to work as software dev. Dont like web development. With that said I still like html and css. Web-field just feels kinda awkward. oO

  • @DonWojtasI
    @DonWojtasI 3 роки тому

    I had a problem with an exception that occured when clicking on an empty space within the Treeview.
    After some googling changing selectItem() to selectItem(MouseEvent event) fixed the issue.

    • @spen9579
      @spen9579 2 роки тому

      youre a real hero, you just saved me hours of desperate troubleshooting. : )

  • @Gurryg
    @Gurryg 2 роки тому +4

    Great tutorial, thanks for the upload!
    For anyone else having issues with adding images to their treeview, use the 'getClass().getResourceAsStream()' as follows:
    TreeItem rootItem = new TreeItem("Files", new ImageView(new Image(getClass().getResourceAsStream("folder_icon.png"))));

    • @MyTEAC_UP
      @MyTEAC_UP 2 роки тому

      thanks bro

    • @benderbg
      @benderbg Рік тому

      Thanks for the tip. I still had error "Cannot infer type arguments for TreeItem" until I manually added imports:
      import javafx.scene.image.Image;
      import javafx.scene.image.ImageView;
      I didnt get any tip from IDE. Might help someone else in 2023.

    • @mclosz3375
      @mclosz3375 Рік тому

      thanks so much@@benderbg

    • @paulstoltjr3783
      @paulstoltjr3783 Рік тому

      thanks for the tip. I was having trouble getting the image to show

  • @emreozogul5052
    @emreozogul5052 2 роки тому

    thanks bro

  • @Qw3rtykid3
    @Qw3rtykid3 2 роки тому

    I keep getting a NullPointerException when the controller is initializing for the treeView fxml variable (I have definitely tagged the TreeView with the fx:id="treeView" tag)_

  • @ethanalkout949
    @ethanalkout949 3 роки тому

    First