过去我在这个论坛上阅读了许多提示,提示' n技巧。 现在我试着在这里问我的第一个问题: - )
我对JavaFX有点新意,但对Java来说并不陌生。我试图将应用程序从Swing移植到JavaFX,因此它会更加花哨和可定制。
该程序的主要任务是显示两棵树并进行比较。 为了更容易,我在旧的应用程序中实现了一个监听器和celleditor来可视化差异,当你在一棵树中选择一个Item时,它会尝试在另一个树中找到相应的Item。如果它不可见,它将是可见的。
显示当前应用程序的一个屏幕截图: 在开始之后看起来像这样 在左侧选择时,在右侧自动选择,反之亦然: Old application
现在我尝试在JavaFX中实现相同的功能: 但是使用这段代码:
public void setOtherTree(TreeView<CompareNode> otherTree) {
if (log.isTraceEnabled()) {
log.trace("setOtherTree("+otherTree+")");
}
this.otherTree = otherTree;
tree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
tree.getSelectionModel().selectedItemProperty().addListener((observable,oldValue,newValue) -> selectInOtherTree(newValue));
}
public void selectInOtherTree(TreeItem<CompareNode> newValue) {
if (log.isDebugEnabled()) {
log.debug(logPrefix + " " + String.format("selectInOtherTree:newValue".replaceAll(", ", "=%s, ") + "=%s", newValue));
}
otherTree.getSelectionModel().clearSelection();
otherTree.getSelectionModel().select(newValue);
}
它总是选择索引库上的项目: Problem in JavaFX
我是否遗漏了某些内容,或者它是JDK 1.8u45(我目前正在使用)中的错误?
CompareNode类覆盖toString,equals和hash
正如我所看到的,我需要10个声誉来发布图片:-(所以我希望我的文字能够详细解释它,或者你可以按照链接。对不起
非常感谢任何帮助。
亲切的问候 安德烈亚斯
2015-07-10更新: 显示旧应用程序的两个屏幕截图:
新应用程序的屏幕截图:
感谢声誉: - )
2015-09-22更新
与此同时,我创建了一个小型演示应用程序来演示它。
TreeItemString.java
package application;
import javafx.scene.control.TreeItem;
public class TreeItemString extends TreeItem<String> {
public TreeItemString(String value) {
this.setValue(value);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
return this.getValue().equals(((TreeItemString) obj).getValu());
}
@Override
public String toString() {
return this.getValue();
}
@Override
public int hashCode() {
return this.getValue().hashCode();
}
}
MainApp.java
package application;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public void selectInOtherTree(TreeItem<String> toSelect, TreeView otherTree, TreeView thisTree) {
System.out.println("Othertree : " + toSelect);
System.out.println("Me : " + thisTree.getRow(toSelect));
System.out.println("Other: " + otherTree.getRow(toSelect));
otherTree.getSelectionModel().select(toSelect);
}
public TreeItemString newRoot(TreeView<String> tree, Label label, String labelPrefix) {
//Create TreeItems for the Hierarchy of the TreeView
TreeItemString royalRoot = new TreeItemString("Queen Elizabeth - Prince Philip");
TreeItemString Charlie = new TreeItemString("Prince Charles - Princess Diana");
TreeItemString Annie = new TreeItemString("Princess Anne - Mark Phillips");
TreeItemString Andy = new TreeItemString("Prince Andrew - Sarah Ferguson");
TreeItemString Eddie = new TreeItemString("Prince Edward - Sophie");
//Populate the TreeItem to be used as the root with the other TreeItems
royalRoot.getChildren().addAll(Charlie, Annie, Andy, Eddie);
//Populate the other TreeItems with more TreeItems
//to build the family tree
Charlie.getChildren().addAll(
new TreeItemString("Prince William"),
new TreeItemString("Prince Henry"));
Annie.getChildren().addAll(
new TreeItemString("Peter Phillips"),
new TreeItemString("Zara Phillips"));
Andy.getChildren().addAll(
new TreeItemString("Princess Beatrice"),
new TreeItemString("Princess Eugenie"));
Eddie.getChildren().addAll(
new TreeItemString("Lady Louise"),
new TreeItemString("Viscount Severn"));
return royalRoot;
}
@Override
public void start(Stage primaryStage) {
final String leftTreeLabelPrefix= "Selected Tree Item From left Tree: \n";
final String rightTreeLabelPrefix = "Selected Tree Item From right Tree: \n";
//Use HBOX and VBOX layout panes to space out the controls
//in a single row
HBox treeBox = new HBox();
VBox labelBox = new VBox(30);
HBox controlBox = new HBox(10);
//Create labels to highlight the selected items from the TreeViews
final Label leftTreeLabel = new Label(leftTreeLabelPrefix);
final Label rightTreeLabel = new Label(rightTreeLabelPrefix);
//Create and empty TreeView
TreeView<String> leftTree = new TreeView<String>();
//Use the setRoot method to set the root TreeItem
// duckTree.setRoot(duckRoot);
leftTree.setRoot(newRoot(leftTree,leftTreeLabel, "Selected Tree Item From left Tree: \n"));
//Create a TreeView using the root TreeItem
TreeView<String> rightTree = new TreeView<String>();
rightTree.setRoot(newRoot(rightTree,rightTreeLabel, "Selected Tree Item From right Tree: \n"));
rightTree.getSelectionModel().selectedItemProperty().addListener((observable,oldValue,newValue) -> selectInOtherTree(newValue, leftTree, rightTree));
leftTree.getSelectionModel().selectedItemProperty().addListener((observable,oldValue,newValue) -> selectInOtherTree(newValue, rightTree,leftTree));
//Add the TreeViews to the HBox
treeBox.getChildren().add(leftTree);
treeBox.getChildren().add(rightTree);
//Add the labels to the VBox
labelBox.getChildren().add(leftTreeLabel);
labelBox.getChildren().add(rightTreeLabel);
//Add the HBox and VBox to another HBox to
//position the layout panes
controlBox.getChildren().add(treeBox);
controlBox.getChildren().add(labelBox);
//Add the main HBOX layout pane to the scene
Scene scene = new Scene(controlBox, 800, 250);
Platform.setImplicitExit(false);
Thread backGroundThread = new Thread(() -> {
try {
Thread.sleep(5000);
System.out.println("Select left Viscount Severn");
leftTree.getRoot().setExpanded(true);
leftTree.getRoot().getChildren().get(1).setExpanded(true);
leftTree.getRoot().getChildren().get(2).setExpanded(true);
leftTree.getRoot().getChildren().get(3).setExpanded(true);
TreeItemString toSearch = new TreeItemString("Prince Edward - Sophie");
System.out.println("true:false for : " + toSearch);
System.out.println("true:false : " + leftTree.getRoot().getChildren().contains(toSearch));
System.out.println("indexOf: " + leftTree.getRoot().getChildren().indexOf(toSearch));
System.out.println("get: " + leftTree.getRoot().getChildren().get(leftTree.getRoot().getChildren().indexOf(toSearch)));
System.out.println("getRow: " + leftTree.getRow(leftTree.getRoot().getChildren().get(leftTree.getRoot().getChildren().indexOf(toSearch))));
leftTree.getSelectionModel().select(new TreeItemString("Princess Anne - Mark Phillips"));
Thread.sleep(5000);
System.out.println("Select right Princess Beatrice");
rightTree.getSelectionModel().select(new TreeItemString("Princess Beatrice"));
} catch (InterruptedException e) {
e.printStackTrace();
}
});
backGroundThread.setDaemon(true);
backGroundThread.start();
//Show the form
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}