1. Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package application; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; public class Main extends Application { @Override public void start(Stage primaryStage) { try { Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml")); Scene scene = new Scene(root); 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); } } | cs |
2. Main.fxml
1 2 3 4 5 6 7 8 9 10 11 | <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.TreeView?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController"> <children> <TreeView fx:id="treeView" layoutX="14.0" layoutY="14.0" onContextMenuRequested="#mouseClick" onMouseClicked="#mouseClick" prefHeight="387.0" prefWidth="469.0" /> </children> </AnchorPane> | cs |
3. MainController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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 MainController implements Initializable{ @FXML TreeView <String> treeView; Image icon = new Image(getClass().getResourceAsStream("/img/folder.png")); @Override public void initialize(URL location, ResourceBundle resources) { ImageView[] iv = new ImageView[7]; for (int i = 0; i < 7; i++) { iv[i] = new ImageView(icon); iv[i].setFitHeight(50); iv[i].setFitWidth(50); } TreeItem<String> root = new TreeItem<>("Root", iv[0]); root.setExpanded(true); // 처음부터 노드가 열려있도록 한다. TreeItem<String> nodeA = new TreeItem<>("node A", iv[1]); TreeItem<String> nodeB = new TreeItem<>("node B", iv[2]); TreeItem<String> nodeC = new TreeItem<>("node C", iv[3]); root.getChildren().addAll(nodeA, nodeB, nodeC); /*root.getChildren().add(nodeA); root.getChildren().add(nodeB); root.getChildren().add(nodeC);*/ nodeA.setExpanded(true); TreeItem<String> nodeA1 = new TreeItem<>("node A1", iv[4]); TreeItem<String> nodeB1 = new TreeItem<>("node B1", iv[5]); TreeItem<String> nodeC1 = new TreeItem<>("node C1", iv[6]); nodeA.getChildren().addAll(nodeA1, nodeB1, nodeC1); treeView.setRoot(root); } public void mouseClick(MouseEvent mouseEvent) { if (mouseEvent.getClickCount() == 2){ TreeItem<String> item = treeView.getSelectionModel().getSelectedItem(); System.out.println(item.getValue()); } } } | cs |
'Java > JavaFx' 카테고리의 다른 글
JavaFX 17. Properties (0) | 2016.08.25 |
---|---|
JavaFX 16. FileChooser (0) | 2016.08.25 |
JavaFx 14. TreeView 사용하기 (0) | 2016.08.17 |
JavaFx 13. ListView 사용하기 (0) | 2016.08.17 |
JavaFx 12. ComboBox 사용하기 (0) | 2016.08.17 |