@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");
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.
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);
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.
*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
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.
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"))));
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.
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)_
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());
}
}
}
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.
Thanks for your great Explanation ☺️
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);
I'm lucky that I have found this channel.
It's like finding a golden egg in a easter egg hunt.
Yo Bro Thanks A Lot I learnt Java Just Because Of You
Thank you bro you are the best
Thanks to share
Amazing video ! is there a chance you can explain how to drag and drop treeview items wthin the treeview ? All the best !
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.
*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
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.
youre a real hero, you just saved me hours of desperate troubleshooting. : )
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"))));
thanks bro
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.
thanks so much@@benderbg
thanks for the tip. I was having trouble getting the image to show
thanks bro
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)_
did you fix it?
First