Java 树状是一种数据结构,它由节点和边组成,其中节点表示数据,而边则表示两个节点之间的关系。树的特征是它有一个根节点,从根节点出发可以到达其他所有的节点。
Java 树的应用非常广泛,它可以用来存储和检索大量数据。例如,在文件处理中,树可以用来存储文件夹和文件之间的关系。此外,在图形界面开发中,树也可以用来显示不同功能之间的关系。
Java 树还可以用于搜索引擎开发中。例如,Google 在它的 PageRank 算法中使用了一个树来衡量不同页面之间的相似性。此外,在语法分析中也使用了树来表征语法关系。
public class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode(int val) { this.val = val; } public void insert(int data) { if (data <= val) { if (left == null) { left = new TreeNode(data); } else { left.insert(data); } } else { if (right == null) { right = new TreeNode(data); } else { right.insert(data); } } } public void inOrder() { if (left != null) { left.inOrder(); } System.out.println(val); if (right != null) { right.inOrder(); } } }
JavaFX TreeTableView在表列中渲染数据的层次结构。
TreeTableView组件组合了TreeView和TableView控件。
import javafx.application.Application; import javafx.beans.property.ReadOnlyStringWrapper; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableColumn.CellDataFeatures; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeTableView; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { final Scene scene = new Scene(new Group(), 200, 400); Group sceneRoot = (Group) scene.getRoot(); TreeItem<String> childNode1 = new TreeItem<>("Node 1"); TreeItem<String> childNode2 = new TreeItem<>("Node 2"); TreeItem<String> childNode3 = new TreeItem<>("Node 3"); TreeItem<String> root = new TreeItem<>("Root"); root.setExpanded(true); root.getChildren().setAll(childNode1, childNode2, childNode3); TreeTableColumn<String, String> column = new TreeTableColumn<>("Column"); column.setPrefWidth(150); column.setCellValueFactory((CellDataFeatures<String, String> p) -> new ReadOnlyStringWrapper( p.getValue().getValue())); TreeTableView<String> treeTableView = new TreeTableView<>(root); treeTableView.getColumns().add(column); sceneRoot.getChildren().add(treeTableView); stage.setScene(scene); stage.show(); } }
上面的代码生成以下结果。
import java.util.Arrays; import java.util.List; import javafx.application.Application; import javafx.beans.property.ReadOnlyStringWrapper; import javafx.beans.property.SimpleStringProperty; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableView; import javafx.stage.Stage; public class Main extends Application { List<Employee> employees = Arrays.<Employee> asList(new Employee( "Ethan Williams", "ethan.williams@example.com"), new Employee( "Emma Jones", "emma.jones@example.com"), new Employee("Michael Brown", "michael.brown@example.com"), new Employee("Anna Black", "anna.black@example.com"), new Employee("Rodger York", "roger.york@example.com"), new Employee("Susan Collins", "susan.collins@example.com")); final TreeItem<Employee> root = new TreeItem<>(new Employee( "Sales Department", "")); public static void main(String[] args) { Application.launch(Main.class, args); } @Override public void start(Stage stage) { root.setExpanded(true); employees.stream().forEach((employee) -> { root.getChildren().add(new TreeItem<>(employee)); }); Scene scene = new Scene(new Group(), 400, 400); Group sceneRoot = (Group) scene.getRoot(); TreeTableColumn<Employee, String> empColumn = new TreeTableColumn<>( "Employee"); empColumn.setPrefWidth(150); empColumn .setCellValueFactory(( TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper( param.getValue().getValue().getName())); TreeTableColumn<Employee, String> emailColumn = new TreeTableColumn<>( "Email"); emailColumn.setPrefWidth(190); emailColumn .setCellValueFactory(( TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper( param.getValue().getValue().getEmail())); TreeTableView<Employee> treeTableView = new TreeTableView<>(root); treeTableView.getColumns().setAll(empColumn, emailColumn); sceneRoot.getChildren().add(treeTableView); stage.setScene(scene); stage.show(); } public class Employee { private SimpleStringProperty name; private SimpleStringProperty email; public SimpleStringProperty nameProperty() { if (name == null) { name = new SimpleStringProperty(this, "name"); } return name; } public SimpleStringProperty emailProperty() { if (email == null) { email = new SimpleStringProperty(this, "email"); } return email; } private Employee(String name, String email) { this.name = new SimpleStringProperty(name); this.email = new SimpleStringProperty(email); } public String getName() { return name.get(); } public void setName(String fName) { name.set(fName); } public String getEmail() { return email.get(); } public void setEmail(String fName) { email.set(fName); } } }
上面的代码生成以下结果。
treeTableView.setTableMenuButtonVisible(true)启用表格菜单按钮,以便用户可以切换表列的可见性。 该方法将“+”按钮添加到表头。
import java.util.Arrays; import java.util.List; import javafx.application.Application; import javafx.beans.property.ReadOnlyStringWrapper; import javafx.beans.property.SimpleStringProperty; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeTableColumn; import javafx.scene.control.TreeTableView; import javafx.stage.Stage; public class Main extends Application { List<Employee> employees = Arrays.<Employee> asList(new Employee( "Ethan Williams", "ethan.williams@example.com"), new Employee( "Emma Jones", "emma.jones@example.com"), new Employee("Michael Brown", "michael.brown@example.com"), new Employee("Anna Black", "anna.black@example.com"), new Employee("Rodger York", "roger.york@example.com"), new Employee("Susan Collins", "susan.collins@example.com")); final TreeItem<Employee> root = new TreeItem<>(new Employee( "Sales Department", "")); public static void main(String[] args) { Application.launch(Main.class, args); } @Override public void start(Stage stage) { root.setExpanded(true); employees.stream().forEach((employee) -> { root.getChildren().add(new TreeItem<>(employee)); }); Scene scene = new Scene(new Group(), 400, 400); Group sceneRoot = (Group) scene.getRoot(); TreeTableColumn<Employee, String> empColumn = new TreeTableColumn<>( "Employee"); empColumn.setPrefWidth(150); empColumn .setCellValueFactory(( TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper( param.getValue().getValue().getName())); TreeTableColumn<Employee, String> emailColumn = new TreeTableColumn<>( "Email"); emailColumn.setPrefWidth(190); emailColumn .setCellValueFactory(( TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper( param.getValue().getValue().getEmail())); TreeTableView<Employee> treeTableView = new TreeTableView<>(root); treeTableView.getColumns().setAll(empColumn, emailColumn); treeTableView.setTableMenuButtonVisible(true); sceneRoot.getChildren().add(treeTableView); stage.setScene(scene); stage.show(); } public class Employee { private SimpleStringProperty name; private SimpleStringProperty email; public SimpleStringProperty nameProperty() { if (name == null) { name = new SimpleStringProperty(this, "name"); } return name; } public SimpleStringProperty emailProperty() { if (email == null) { email = new SimpleStringProperty(this, "email"); } return email; } private Employee(String name, String email) { this.name = new SimpleStringProperty(name); this.email = new SimpleStringProperty(email); } public String getName() { return name.get(); } public void setName(String fName) { name.set(fName); } public String getEmail() { return email.get(); } public void setEmail(String fName) { email.set(fName); } } }
我们可以通过使用TreeTableView类的setShowRoot方法显示或隐藏根树项目。
treeTableView.setShowRoot(false);
上面的代码生成以下结果。
我们可以通过点击列标题对数据进行排序。
设置列的降序排序模式
aColumn.setSortType(TreeTableColumn.SortType.DESCENDING);
设置列的升序排序模式
aColumn.setSortType(TreeTableColumn.SortType.ASCENDING);
将排序模式应用于所有树项目
treeTableView.setSortMode(TreeSortMode.ALL_DESCENDANTS);
仅将排序模式应用于第一级节点
treeTableView.setSortMode(TreeSortMode.ONLY_FIRST_LEVEL);
TreeTableView类的默认选择模型是SelectionMode.SINGLE。
要启用树项目和单元格的多重选择,请使用setSelectionModel和setCellSelectionEnabled方法的组合。
启用单元格的多重选择
treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); treeeTableView.getSelectionModel().setCellSelectionEnabled(true);
JavaFX教程 -JavaFX散点图散点图是一个双轴图表,将其数据作为一组点显示。每个点由X和Y值定义。import javafx.application.Appl...
Java脚本教程 -Java脚本输出Nashorn提供了三种函数来输出文本到标准输出:print() 函数printf() 函数echo() 函数 echo()函数和pr...
Java脚本 教程-Java脚本数据类型Nashorn中的数据类型有两种类型:基本类型和对象类型。基本类型包括以下五种数据类型:UndefinedNu...
Java教程 -Java While语句while循环在其控制条件为真时重复语句或块。Java while循环这是它的一般形式:while(condition) {// bod...
Java isDigit() 方法JavaCharacter类isDigit() 方法用于判断指定字符是否为数字。语法boolean isDigit(char ch)参数ch -- 要测试...