我正在尝试从TreeView中获取当前存在的TreeCell的列表。我当前的尝试如下所示:
private Set<MyTreeCell> getMyTreeCells(){
try {
@SuppressWarnings("unchecked")
Field f = ((VirtualContainerBase<TreeView<myNode>, TreeViewBehavior<myNode>, MyTreeCell>) myTreeView.skinProperty().get()).getClass().getDeclaredField("flow");
f.setAccessible(true);
@SuppressWarnings("unchecked")
Field g = ((VirtualFlow<MyTreeCell>) f.get(((VirtualContainerBase<TreeView<myNode>, TreeViewBehavior<myNode>, ProofTreeCell>) proofTreeView.skinProperty().get()))).getClass().getDeclaredField("cells");
g.setAccessible(true);
@SuppressWarnings("unchecked")
ArrayLinkedList<MyTreeCell> l = (ArrayLinkedList<MyTreeCell>) g.get(((VirtualFlow<MyTreeCell>) f.get(((TreeViewSkin<myNode>) myTreeView.skinProperty().get()))));
Set<myTreeCell> s = new HashSet<>();
s.addAll(l);
return s;
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
不幸的是,它抛出
java.lang.NoSuchFieldException: flow
。但为什么?显然,字段flow
存在于类VirtualContainerBase中。请注意,我只是为了好玩而写,而不是实际用于生产代码中。我已经有一个更清洁的解决方案。
最佳答案
VirtualContainerBase
声明flow
,但是myTreeView.skinProperty().get().getClass()
不返回VirtualContainerBase.class
,它返回TreeViewSkin.class
。由于getDeclaredField()
仅返回在该类中声明的字段,而不返回继承的字段,因此您将获得异常。
我想你可以做
Field f = VirtualContainerBase.class.getDeclaredField("flow");
Field g = VirtualFlow.class.getDeclaredField("cells");
您还可以消除很多铸造工作,例如以下示例:
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.sun.javafx.scene.control.skin.VirtualContainerBase;
import com.sun.javafx.scene.control.skin.VirtualFlow;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class GetTreeCellsByReflection extends Application {
@SuppressWarnings({ "restriction", "unchecked" })
@Override
public void start(Stage primaryStage) {
TreeView<Integer> tree = createRandomTree(100);
Button findCells = new Button("Find cells");
findCells.setOnAction(e -> {
try {
Field flowField = VirtualContainerBase.class.getDeclaredField("flow");
flowField.setAccessible(true);
Field cellsField = VirtualFlow.class.getDeclaredField("cells");
cellsField.setAccessible(true);
Object flow = flowField.get(tree.getSkin());
((Iterable<TreeCell<?>>) cellsField.get(flow)).forEach(System.out::println);;
} catch (Exception exc) {
exc.printStackTrace();
}
});
BorderPane root = new BorderPane(tree);
BorderPane.setAlignment(findCells, Pos.CENTER);
BorderPane.setMargin(findCells, new Insets(5));
root.setBottom(findCells);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private TreeView<Integer> createRandomTree(int numItems) {
Random rng = new Random();
TreeItem<Integer> root = new TreeItem<>(0);
List<TreeItem<Integer>> items = new ArrayList<>();
items.add(root);
for (int i = 1 ; i < numItems ; i++) {
TreeItem<Integer> item = new TreeItem<>(i);
items.get(rng.nextInt(items.size())).getChildren().add(item);
items.add(item);
}
return new TreeView<>(root);
}
public static void main(String[] args) {
launch(args);
}
}