- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试开发一个文件复制应用程序。我已经使用文件系统上的当前目录创建了一个复选框树项。
但是当我选择第一个节点(c:/目录)时,需要很长时间。如何轻松快速地选择所有目录?
这是我的第一个 FXML 加载类:
@Override
public void initialize(URL location, ResourceBundle resources) {
TreeView pathTree = new MyFileTreeView().getMyFilePathTree();
vBoxFileTree.getChildren().add(pathTree);
}
这是我的 TreeView 组件:
public class MyFileTreeView {
private TreeView<Path> filePathTree;
private List<Path> rootDirectories;
private Logger logger = Logger.getLogger(MyFileTreeView.class);
public MyFileTreeView() {
rootDirectories = new ArrayList<>();
Iterable<Path> roots = FileSystems.getDefault().getRootDirectories();
for (Path root : roots) {
rootDirectories.add(root);
}
}
public TreeView getMyFilePathTree() {
if (filePathTree == null) {
filePathTree = new TreeView<>(getRootItem());
filePathTree.setPrefHeight(600.0d);
filePathTree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
filePathTree.setCellFactory((TreeView<Path> t) -> new TreeCellImpl());
filePathTree.setShowRoot(false);
}
return filePathTree;
}
private TreeItem getRootItem() {
TreeItem rootItem = new TreeItem();
for (Path path : rootDirectories) {
MyFileTreeItem item = new MyFileTreeItem(path);
item.setIndependent(false);
rootItem.getChildren().add(item);
logger.info(path.toString() + " directory has been added to fileTree!");
}
return rootItem;
}
}
这是树项目:
public class MyFileTreeItem extends CheckBoxTreeItem<Path> {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
public MyFileTreeItem(Path path) {
super(path);
}
@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
Path path = getValue();
isLeaf = Files.isRegularFile(path);
}
return isLeaf;
}
@Override
public ObservableList<TreeItem<Path>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
private ObservableList<TreeItem<Path>> buildChildren(CheckBoxTreeItem<Path> treeItem) {
Path path = treeItem.getValue();
if ((path != null) && (Files.isDirectory(path))) {
try (Stream<Path> pathStream = Files.list(path)) {
return pathStream
.map(p -> new MyFileTreeItem(p))
.collect(Collectors.toCollection(() ->
FXCollections.observableArrayList()));
} catch (IOException e) {
}
}
return FXCollections.emptyObservableList();
}
}
更新:
感谢@fabian,我添加了不确定属性
public class FileTreeItem extends TreeItem<Path> {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
private BooleanProperty selected;
private BooleanProperty indeterminate;
public FileTreeItem(Path path) {
this(path, false, false);
}
protected FileTreeItem(Path path, boolean selected, boolean indeterminate) {
super(path);
this.selected = new SimpleBooleanProperty(selected);
this.indeterminate = new SimpleBooleanProperty(indeterminate);
this.selected.addListener((o, oldValue, newValue) -> {
if (!isLeaf() && !isFirstTimeChildren) {
if (!isIndeterminate()) {
for (TreeItem<Path> ti : getChildren()) {
((FileTreeItem) ti).setSelected(newValue);
}
}
if (isIndeterminate() && newValue) {
setIndeterminate(false);
for (TreeItem<Path> ti : getChildren()) {
((FileTreeItem) ti).setSelected(newValue);
}
}
}
if (!newValue) {
if (getParent() instanceof FileTreeItem) {
FileTreeItem parent = (FileTreeItem) getParent();
parent.setIndeterminate(true);
parent.setSelected(false);
}
} else {
if (getParent() instanceof FileTreeItem) {
boolean allChildSelected = true;
FileTreeItem parent = (FileTreeItem) getParent();
for (TreeItem<Path> child : parent.getChildren()) {
if (!((FileTreeItem) child).isSelected()) {
allChildSelected = false;
break;
}
}
if (allChildSelected && !parent.isSelected()) {
setIndeterminate(false);
parent.setIndeterminate(false);
parent.setSelected(true);
}
}
}
});
}
@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
Path path = getValue();
isLeaf = Files.isRegularFile(path);
}
return isLeaf;
}
@Override
public ObservableList<TreeItem<Path>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
private List<TreeItem<Path>> buildChildren(FileTreeItem treeItem) {
Path path = treeItem.getValue();
if ((path != null) && (Files.isDirectory(path))) {
final boolean select = treeItem.isSelected();
boolean indeterminate = treeItem.isIndeterminate();
try (Stream<Path> pathStream = Files.list(path)) {
List<TreeItem<Path>> res = new ArrayList<>();
pathStream
.map(p -> new FileTreeItem(p, select, indeterminate))
.forEach(res::add);
return res;
} catch (IOException e) {
}
}
return Collections.emptyList();
}
public boolean isSelected() {
return selected.get();
}
public BooleanProperty selectedProperty() {
return selected;
}
public void setSelected(boolean value) {
selected.set(value);
}
public boolean isIndeterminate() {
return indeterminate.get();
}
public BooleanProperty indeterminateProperty() {
return indeterminate;
}
public void setIndeterminate(boolean indeterminate) {
this.indeterminate.set(indeterminate);
}
}
最佳答案
当您更改 CheckBoxTreeItem
的选定状态时,子项的状态将设置为相同的值。这意味着调用了 getChildren 方法,并且还为所有子项设置了 selected 属性。这样您就可以有效地对目录的所有内容进行深度优先遍历。
因此,您需要直接扩展 TreeItem
并实现所需的属性。您需要确保在更新 selected
属性时,仅当已调用 getChildren
时才迭代子项:
public class FileTreeItem extends TreeItem<Path> {
private boolean isLeaf;
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
private final BooleanProperty selected;
private final BooleanProperty indeterminate;
protected FileTreeItem(Path path, boolean selected) {
super(path);
this.indeterminate = new SimpleBooleanProperty();
this.selected = new SimpleBooleanProperty(selected);
this.selected.addListener((o, oldValue, newValue) -> {
if (!updating) {
if (!isLeaf() && !isFirstTimeChildren) {
// propagate selection to children if they were created yet
for (TreeItem<Path> ti : getChildren()) {
FileTreeItem fti = (FileTreeItem) ti;
fti.setSelected(newValue);
}
}
// update ancestors
TreeItem<Path> parent = getParent();
while ((parent instanceof FileTreeItem)
&& updateAncestorState((FileTreeItem) parent)) {
parent = parent.getParent();
}
}
});
}
/**
* flag preventing circular calls during update.
*/
private boolean updating;
protected static boolean updateAncestorState(FileTreeItem item) {
List<TreeItem<Path>> children = item.getChildren();
boolean hasUnselected = false;
boolean hasSelected = false;
for (Iterator<TreeItem<Path>> it = children.iterator();!(hasSelected && hasUnselected) && it.hasNext();) {
TreeItem<Path> ti = it.next();
FileTreeItem child = (FileTreeItem) ti;
if (child.isSelected()) {
hasSelected = true;
} else {
hasUnselected = true;
if (child.isIndeterminate()) {
hasSelected = true;
}
}
}
item.updating = true;
boolean changed = false;
if (hasUnselected) {
if (item.isSelected() || item.isIndeterminate() != hasSelected) {
changed = true;
item.setSelected(false);
item.setIndeterminate(hasSelected);
}
} else {
if (!item.isSelected()) {
changed = true;
item.setSelected(true);
}
item.setIndeterminate(false);
}
item.updating = false;
return changed;
}
public FileTreeItem(Path path) {
this(path, false);
}
@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
Path path = getValue();
isLeaf = Files.isRegularFile(path);
}
return isLeaf;
}
@Override
public ObservableList<TreeItem<Path>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
private List<TreeItem<Path>> buildChildren(FileTreeItem treeItem) {
Path path = treeItem.getValue();
if ((path != null) && (Files.isDirectory(path))) {
final boolean select = treeItem.isSelected();
try (Stream<Path> pathStream = Files.list(path)) {
List<TreeItem<Path>> res = new ArrayList<>();
pathStream
.map(p -> new FileTreeItem(p, select))
.forEach(res::add);
return res;
} catch (IOException e) {
}
}
return Collections.emptyList();
}
/* methods for selected & indeterminate properties */
}
编辑
显示不确定
属性需要您实现自己的TreeCell
:
public class FileItemCheckBoxTreeCell extends TreeCell<Path> {
private BooleanProperty oldSelectedProperty;
private BooleanProperty oldIndeterminateProperty;
private final CheckBox checkBox;
private final StringConverter<TreeItem<Path>> converter;
public FileItemCheckBoxTreeCell(StringConverter<TreeItem<Path>> converter) {
if (converter == null) {
throw new IllegalArgumentException();
}
this.converter = converter;
this.checkBox = new CheckBox();
}
@Override
protected void updateItem(Path item, boolean empty) {
// clear old binding
if (oldSelectedProperty != null) {
checkBox.selectedProperty().unbindBidirectional(oldSelectedProperty);
checkBox.indeterminateProperty().unbindBidirectional(oldIndeterminateProperty);
oldSelectedProperty = null;
oldIndeterminateProperty = null;
}
checkBox.indeterminateProperty().unbind();
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText("");
} else {
TreeItem<Path> treeItem = getTreeItem();
setText(converter.toString(treeItem));
if (treeItem instanceof FileTreeItem) {
setGraphic(checkBox);
FileTreeItem fti = (FileTreeItem) treeItem;
oldSelectedProperty = fti.selectedProperty();
oldIndeterminateProperty = fti.indeterminateProperty();
checkBox.selectedProperty().bindBidirectional(oldSelectedProperty);
checkBox.indeterminateProperty().bindBidirectional(oldIndeterminateProperty);
} else {
setGraphic(null);
}
}
}
public static Callback<TreeView<Path>, TreeCell<Path>> forTreeView(StringConverter<TreeItem<Path>> converter) {
if (converter == null) {
throw new IllegalArgumentException();
}
return tv -> new FileItemCheckBoxTreeCell(converter);
}
}
public TreeView getMyFilePathTree() {
if (filePathTree == null) {
filePathTree = new TreeView<>(getRootItem());
filePathTree.setPrefHeight(600.0d);
filePathTree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
// tell cell factory to use the selected property for checkbox
filePathTree.setCellFactory(FileItemCheckBoxTreeCell.forTreeView(new StringConverter<TreeItem<Path>>() {
@Override
public String toString(TreeItem<Path> object) {
if (object == null) {
return "";
}
Path p = object.getValue();
if (p == null) {
return "";
}
p = p.getFileName();
return p == null ? object.getValue().toString() : p.toString();
}
@Override
public TreeItem<Path> fromString(String string) {
throw new UnsupportedOperationException();
}
}));
filePathTree.setShowRoot(false);
}
return filePathTree;
}
关于JavaFx CheckBoxTreeItem<Path> 选择根项错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086335/
是否有某种方法可以使用 JPA 或 Hibernate Crtiteria API 来表示这种 SQL?或者我应该将其作为 native 执行吗? SELECT A.X FROM (SELECT X,
在查询中, select id,name,feature,marks from (....) 我想删除其 id 在另一个 select 语句中存在的那些。 从 (...) 中选择 id 我是 sql
我想响应用户在 select 元素中选择一个项目。然而这个 jQuery: $('#platypusDropDown').select(function () { alert('You sel
这个问题在这里已经有了答案: SQL select only rows with max value on a column [duplicate] (27 个回答) 关闭8年前。 我正在学习 SQL
This question already has answers here: “Notice: Undefined variable”, “Notice: Undefined index”, and
我在 php 脚本中调用 SQL。有时“DE”中没有值,如果是这种情况我想从“EN”中获取值 应该是这样的,但不是这样的 IF (EXISTS (SELECT epf_application_deta
这可能是一个奇怪的问题,但不知道如何研究它。执行以下查询时: SELECT Foo.col1, Foo.col2, Foo.col3 FROM Foo INNER JOIN Bar ON
如何在使用 Camera.DestinationType.FILE_URI. 时在 phonegap camera API 中同时选择或拾取多个图像我能够一次只选择一张图像。我可以使用 this 在
这是一个纯粹的学术问题。这两个陈述实际上是否相同? IF EXISTS (SELECT TOP 1 1 FROM Table1) SELECT 1 ELSE SELECT 0 相对 IF EXIS
我使用 JSoup 来解析 HTML 响应。我有多个 Div 标签。我必须根据 ID 选择 Div 标签。 我的伪代码是这样的 Document divTag = Jsoup.connect(link
我正在处理一个具有多个选择框的表单。当用户从 selectbox1 中选择一个选项时,我需要 selectbox2 active 的另一个值。同样,当他选择 selectbox2 的另一个值时,我需要
Acme Inc. Christa Woods Charlotte Freeman Jeffrey Walton Ella Hubbard Se
我有一个login.html其中form定义如下: First Initial Plus Last Name : 我的do_authorize如下: "; pri
$.get( 'http://www.ufilme.ro/api/load/maron_online/470', function(data
我有一个下拉列表“磅”、“克”、“千克”和“盎司”。我想要这样一种情况,当我选择 gram 来执行一个函数时,当我在输入字段中输入一个值时,当我选择 pounds 时,我想要另一个函数来执行时我在输入
我有一个 GLSL 着色器,它从输入纹理的 channel 之一(例如 R)读取,然后写入输出纹理中的同一 channel 。该 channel 必须由用户选择。 我现在能想到的就是使用一个 int
我想根据下拉列表中的选定值生成输入文本框。 Options 2 3 4 5 就在这个选择框之后,一些输入字段应该按照选定的数字出现。 最佳答案 我建议您使用响应式(Reac
我是 SQL 新手,我想问一下如何根据首选项和分组选择条目。 +----------+----------+------+ | ENTRY_ID | ROUTE_ID | TYPE | +------
我有以下表结构: CREATE TABLE [dbo].[UTS_USERCLIENT_MAPPING_USER_LIST] ( [MAPPING_ID] [int] IDENTITY(1,1
我在移除不必要的床单时遇到了问题。我查看了不同的论坛并将不同的解决方案混合在一起。 此宏删除工作表(第一张工作表除外)。 Sub wrong() Dim sht As Object Applicati
我是一名优秀的程序员,十分优秀!