Created
May 4, 2018 17:58
-
-
Save sharifulislam52/e36d5cdebd35d9d2e777758d406569f3 to your computer and use it in GitHub Desktop.
javafx : TreeView with icon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.stage.Stage; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
try { | |
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); | |
Scene scene = new Scene(root,300,300); | |
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.TreeView?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1" fx:controller="application.MainController"> | |
<children> | |
<TreeView fx:id="treeview" layoutX="50.0" layoutY="30.0" onContextMenuRequested="#mouseClick" onMouseClicked="#mouseClick" prefHeight="200.0" prefWidth="200.0" /> | |
</children> | |
</AnchorPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.util.ResourceBundle; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.TreeView; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.control.TreeItem; | |
public class MainController implements Initializable{ | |
@FXML | |
TreeView<String> treeview; | |
Image icon = new Image(getClass().getResourceAsStream("/img/folder.png")); | |
@Override | |
public void initialize(URL arg0, ResourceBundle arg1) { | |
TreeItem<String> root = new TreeItem<>("Root", new ImageView(icon)); | |
root.setExpanded(true); | |
TreeItem<String> nodeA = new TreeItem<>("nodeA", new ImageView(icon)); | |
TreeItem<String> nodeB = new TreeItem<>("nodeB"); | |
TreeItem<String> nodeC = new TreeItem<>("nodeC"); | |
root.getChildren().addAll(nodeA,nodeB,nodeC); | |
TreeItem<String> nodeA1 = new TreeItem<>("nodeA1"); | |
TreeItem<String> nodeB1 = new TreeItem<>("nodeB1"); | |
TreeItem<String> nodeC1 = new TreeItem<>("nodeC1"); | |
nodeA.getChildren().addAll(nodeA1,nodeB1,nodeC1); | |
treeview.setRoot(root); | |
} | |
public void mouseClick(MouseEvent event) { | |
if(event.getClickCount() == 2) { | |
TreeItem<String> item = treeview.getSelectionModel().getSelectedItem(); | |
System.out.println(item.getValue()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment