JavaFx Tutorial For Beginners 14 - JavaFX TreeView
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);
}
}
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/icon.png"));
@Override
public void initialize(URL location, ResourceBundle resources) {
TreeItem<String> root = new TreeItem<>("Root", new ImageView(icon));
root.setExpanded(true);
TreeItem<String> nodeA = new TreeItem<>("node A", new ImageView(icon));
TreeItem<String> nodeB = new TreeItem<>("node B", new ImageView(icon));
TreeItem<String> nodeC = new TreeItem<>("node C", new ImageView(icon));
root.getChildren().addAll(nodeA,nodeB,nodeC);
nodeA.setExpanded(true);
TreeItem<String> nodeA1 = new TreeItem<>("node A 1", new ImageView(icon));
TreeItem<String> nodeB1 = new TreeItem<>("node B 1", new ImageView(icon));
TreeItem<String> nodeC1 = new TreeItem<>("node C 1", new ImageView(icon));
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());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<TreeView fx:id="treeView" layoutX="59.0" layoutY="41.0" onContextMenuRequested="#mouseClick" onMouseClicked="#mouseClick" prefHeight="374.0" prefWidth="389.0" />
</children>
</AnchorPane>
JavaFx Tutorial For Beginners 14 - JavaFX TreeView
Reviewed by Mursal Zheker
on
Sabtu, November 14, 2015
Rating:
Tidak ada komentar: