- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个可折叠的列表,所以我使用的是 TreeView,但较长的字符串会提供水平滚动条而不是自动换行。我已经尝试在 .tree-cell
类上使用 CSS 属性 -fx-wrap-text
但不幸的是似乎没有任何反应。
TreeCell
不是要跨越一行以上吗?如果是这样,还有什么替代方案?
这是它现在的样子:
test.css
.tree-cell {
-fx-wrap-text: true;
-fx-text-fill: blue;
}
subjects.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController">
<Button fx:id="button" layoutX="182.0" layoutY="14.0" onAction="#handleButtonAction" text="Click Me!" />
<TextField fx:id="filterSubject" layoutX="21.0" layoutY="14.0" />
<TreeView fx:id="subjectList" editable="true" layoutX="14.0" layoutY="61.0" prefHeight="200.0" prefWidth="365.0" />
<stylesheets>
<URL value="@test.css" />
</stylesheets>
</AnchorPane>
subjects.txt - 只是一对文本,第一个是主题,第二个是描述。
1
Somewhat long string that I want to wordwrap.
2
Somewhat long string that I want to wordwrap.
3
Somewhat long string that I want to wordwrap.
FXMLDocumentController.java
package javafxapplication1;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TreeView;
import javafx.scene.control.TreeItem;
import java.io.*;
public class FXMLDocumentController implements Initializable {
@FXML
private TreeView<String> subjectList;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
}
private void getSubjects(){
TreeItem<String> subjectRoot = new TreeItem<String> ("Subject");
subjectRoot.setExpanded(true);
try {
BufferedReader fileReader = new BufferedReader(
new FileReader("subjects.txt")
);
String subject = null;
String description = null;
while((subject = fileReader.readLine()) != null) {
description = fileReader.readLine();
TreeItem<String> subjectTree = new TreeItem<String> (subject);
TreeItem<String> descriptionItem = new TreeItem<String> (description);
subjectTree.getChildren().add(descriptionItem);
subjectRoot.getChildren().add(subjectTree);
}
} catch(FileNotFoundException e) {
System.out.println("File not found");
} catch(IOException e) {
e.printStackTrace();
}
subjectList.setRoot(subjectRoot);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
getSubjects();
}
}
JavaFXApplication1.Java
package javafxapplication1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
实际上 CSS 类正在按照您的预期工作。
问题是:TreeView
包含 TreeCell
。这些 TreeCell
将包裹其文本,以防它们没有足够的垂直空间来垂直增长。由于 TreeView
有一个内置的“ScrollPane
”,其中的视口(viewport)将垂直增长,它将为 TreeCell
实例提供足够的空间任何情况下都会增长,因此永远不会启用包装。
为避免这种情况,您可以设置 cell factory手动生成 TreeCell
实例。然后你可以绑定(bind) prefWidthProperty
这些元素的 widthProperty
TreeView
的。
示例
public class WrappedTreeView extends Application {
@Override
public void start(Stage stage) {
final Scene scene = new Scene(new Group(), 200, 400);
Group sceneRoot = (Group) scene.getRoot();
scene.getStylesheets().add(getClass().getResource("application.css").toString());
TreeItem<String> root = new TreeItem<>("Root");
root.setExpanded(true);
TreeItem<String> childNode1 = new TreeItem<>("I am a very long node - the first one -, my text must be wrapped! If it is not wrapped, it's a problem!");
TreeItem<String> childNode2 = new TreeItem<>("I am a very long node - the second one -, my text must be wrapped! If it is not wrapped, it's a problem!");
TreeItem<String> childNode3 = new TreeItem<>("I am a very long node - the third one -, my text must be wrapped! If it is not wrapped, it's a problem!");
root.getChildren().addAll(childNode1, childNode2, childNode3);
childNode2.getChildren().add(new TreeItem<>("And I am a very long embedded node, so my text must be wrapped!"));
TreeView<String> treeView = new TreeView<>(root);
treeView.setCellFactory(item -> {
TreeCell<String> treeCell = new TreeCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null && !empty)
setText(item);
else
setText("");
}
};
treeCell.prefWidthProperty().bind(treeView.widthProperty().subtract(5.0));
return treeCell;
});
treeView.setMaxWidth(200);
sceneRoot.getChildren().add(treeView);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
application.css的内容
.tree-cell {
-fx-wrap-text: true;
-fx-text-fill: blue;
}
以及生成的 TreeView
:
关于Treeview、TreeItem/TreeCell 上的 JavaFX 自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39712661/
我正在使用 GWT ,我有树 Widget.TreeItems 在运行时添加。示例: 1.A 2.B 当我点击A时看起来像 1.A 1.ab 2.cd
我关注这个example to create a simple test with a Table where the foreground color was changed 。这按预期工作。 但是
我正在捕获右键单击事件以显示上下文菜单。我一直无法弄清楚的是,如何在显示上下文菜单之前右键单击实际选择 TreeItem。 感谢所有帮助。 private Tree tree = new Tree(
我目前正在使用 JavaFx-2 的 TreeView代表一个文件系统。 我想启用 drag and drop允许移动操作,但它看起来像 TreeItem不包括拖动事件监听器。我只能在 englobi
我想实现一棵树,里面有可检查的根节点和一些参数(实际上,本例中的参数并不重要)。因此,我制作一棵树,然后放置两列,然后放置一个 TreeItem。然后我需要放置一个带有复选框按钮的 TreeEdito
我有一个 TreeItem(一棵包含 100 多个 TreeItems 元素的树)。我想让用户能够隐藏(或再次显示)一些 TreeItems。 (因此用户只会看到他关心的项目)。 我看不到 TreeI
我正在 JavaFX 中使用 TreeView ,我想知道如何区分类型的节点,我的意思是,我有下一个结构: 1.-Agent 1.1-Use Case Diagram 1.1.1-U
我正在使用 TreeTableView (JavaFX 8)。有一些树节点,必须禁用才能选择。我已经尝试过选择事件,但它不起作用。请找到以下代码以获取更多信息。 treeTableView.getSe
我正在开发 Eclipse 插件。在这里我创建了一个单独的 View ,现在我想格式化树节点的颜色。 这些是 createpartcontrol 方法中的代码。 ScrolledComposite s
我有一个TreeViewer,其中当用户选择一个TreeItem并单击“展开”按钮时,应打开TreeItem下的整个层次结构,直到最后一个叶子找到节点。 我在 TreeViewer 上找到了一个现有方
我有一个 JFace TreeViewer,其中用户可以选择一个 TreeItem 并通过菜单选项检查是否是叶节点。 如何实现这一目标? 最佳答案 您可以通过以下方式从树查看器中获取当前选定的对象:
我正在尝试将包含文件路径的工具提示附加到 TreeItem ,这样当我将鼠标悬停在此 TreeItem 周围时,当我将鼠标悬停在其周围时,它将显示文件路径的文本。这在我的代码中不起作用,因为它提示我无
我有以下类(class): class TreeItem>{ private final ObservableList childs; private T parent; pu
我正在尝试创建一种方法来实现标题所说的功能。假设我有下面的 TreeView ,我想添加一个搜索功能,用户可以在其中给出例如一个值 desktop 并使用该方法获取具有给定值的第一个 TreeItem
有TreeView,每个元素都实现了Viewable接口(interface): public interface Viewable { enum ViewStyle { NE
在我的应用程序中,我有一个由树项组成的树结构。 我想做的是改变某些树元素的背景,如果他们的 userObject 满足某些条件。我遇到的问题是,当根树元素的背景发生变化时(仅根据根级别的树元素满足的标
我制作了这个 TreeView : TreeItem root = new TreeItem<>("roothidden"); treeView.setRoot(root);
编辑 #2:由于它看起来像一个错误,我已经在 javaFx-jira 中发布了错误报告.您必须拥有一个帐户才能访问该问题。如果有新信息,我会及时更新这篇文章。 原帖:我有一个带有按钮和 TreeVie
我有一个 TreeItem目的。 我想在里面放一个按钮。 Button button = new Button("Hello!"); root.setGraphic(button); // root
使用标准 GWT 2.0.3 API,如何向 TreeItem 添加 Clickhandler?我希望实现对服务器的异步调用,以检索展开的结果 TreeItem。 不幸的是FastTree在 GXT
我是一名优秀的程序员,十分优秀!