我正在尝试使用CheckBoxTreeItems制作TreeView。我在网上尝试了教程,但我发现了一个问题。当我折叠CheckBoxTreeItems时,我设置的图像消失了。我用谷歌搜索,但我找不到正确的答案。如果你给我一些想法,那将是我的荣幸。
public class SelectiveController implements Initializable {
@FXML
Button btRefresh;
@FXML
Button btConfirm;
@FXML
Button btCancel;
@FXML
TreeView<String> tvFolderList;
private static final Image folderIcon = new Image("http://icons.iconarchive.com/icons/hopstarter/sleek-xp-basic/16/Folder-icon.png");
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
setUIEventListener();
}
public void setTree() {
CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<String>("View Source Files", new ImageView(folderIcon));
rootItem.setExpanded(true);
for (int i = 0; i < 4; i++) {
CheckBoxTreeItem<String> checkBoxTreeItem = new CheckBoxTreeItem<String>("Sample" + (i + 1), new ImageView(folderIcon));
rootItem.getChildren().add(checkBoxTreeItem);
for(int j=0; j < 2; j++) {
CheckBoxTreeItem<String> subItem = new CheckBoxTreeItem<String>("Sample" + (j + 1), new ImageView(folderIcon));
checkBoxTreeItem.getChildren().add(subItem);
for(int k = 0; k < 2; k++) {
CheckBoxTreeItem<String> subSubItem = new CheckBoxTreeItem<String>("Sample" + (j + 1), new ImageView(folderIcon));
subItem.getChildren().add(subSubItem);
}
}
}
tvFolderList.setEditable(true);
tvFolderList.setRoot(rootItem);
tvFolderList.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
tvFolderList.setShowRoot(true);
}
public void setUIEventListener() {
btCancel.setOnMouseClicked(new EventHandler<Event>() {
@Override
public void handle(Event event) {
// TODO Auto-generated method stub
System.out.println("test");
}
});
btRefresh.setOnMouseClicked(new EventHandler<Event>() {
@Override
public void handle(Event arg0) {
setTree();
}
});
}
}
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Treeview.fxml"));
Parent root = (Parent)loader.load();
Scene scene = new Scene(root,600,500);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("selective UI mock-up");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}