Saturday, November 14, 2015

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>






IT Certification Category (English)640x480

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


Top Online Courses From ProgrammingKnowledge

Python Course http://bit.ly/2vsuMaS
Java Coursehttp://bit.ly/2GEfQMf
Bash Coursehttp://bit.ly/2DBVF0C
Linux Coursehttp://bit.ly/2IXuil0
C Course http://bit.ly/2GQCiD1
C++ Coursehttp://bit.ly/2V4oEVJ
PHP Coursehttp://bit.ly/2XP71WH
Android Coursehttp://bit.ly/2UHih5H
C# Coursehttp://bit.ly/2Vr7HEl
JavaFx Coursehttp://bit.ly/2XMvZWA
NodeJs Coursehttp://bit.ly/2GPg7gA
Jenkins Course http://bit.ly/2Wd4l4W
Scala Coursehttp://bit.ly/2PysyA4
Bootstrap Coursehttp://bit.ly/2DFQ2yC
MongoDB Coursehttp://bit.ly/2LaCJfP
QT C++ GUI Coursehttp://bit.ly/2vwqHSZ