- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我对 ListView
有一点小问题而且我不确定是因为我缺少一些知识还是我的方法有缺陷。不得不承认,在许多可能的情况下,我还不清楚 JavaFX 如何处理布局。
上面的屏幕截图显示了我使用完全相同的代码两次得到的结果,除了 在第二个上一个不可见的形状我用于连贯布局对调试可见.
CellFactory
所涉及的各种类延长 Group
,我尝试了其他一些Parent
到目前为止没有太大的成功。
而不是分享我的StarShape
, StarRow
和其他一些杂项类(如果需要,我很乐意)我写了一个重现该问题的示例。类扩展Application
并覆盖 start(...)
方法如下:
@Override
public void start(Stage primaryStage) throws Exception {
final StackPane root = new StackPane();
final Scene scene = new Scene(root, 400, 600);
final ListView<Boolean> listView = new ListView<>();
listView.setCellFactory(this::cellFactory);
for (int i = 0; i < 5 ; i++) {
listView.getItems().add(true);
listView.getItems().add(false);
}
root.getChildren().add(listView);
primaryStage.setScene(scene);
primaryStage.setTitle("ListView trims the invisible");
primaryStage.show();
}
在哪里 this::cellFactory
是
private ListCell<Boolean> cellFactory(ListView<Boolean> listView) {
return new ListCell<Boolean>() {
@Override
protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
final Rectangle tabShape = new Rectangle();
tabShape.setHeight(20);
tabShape.setWidth(40);
tabShape.setVisible(item);
final Label label = new Label(item.toString());
label.setLayoutX(40);
final Group cellRoot = new Group();
cellRoot.getChildren().add(tabShape);
cellRoot.getChildren().add(label);
setGraphic(cellRoot);
}
}
};
}
上面将显示 ListView<Boolean>
true
前面有黑色形状项目(因为 tabShape.setVisible(item);
位)。 false
元素看起来像普通的 Label
物体仿佛在其Group
中的隐形形状不存在(但它存在)。
对此进行调试,结果发现具有不可见形状的组被赋予负的 layoutX 属性值。因此Label
控件没有像我希望的那样对齐。当我调用 setLayoutX
时不会发生这种情况和 setLayoutY
在 ListView
之外(不可见的形状会强制偏移),但它可能不是唯一会发生这种情况的地方。
发生了什么以及如何避免它?或者,我猜我正在接近这个错误,正确的方法是什么?换句话说,我应该问什么问题而不是这个?
最佳答案
取自 @dlatikay's comment ,而不是将占位符项目设置为不可见,您可以通过将其不透明度设置为 0.0
来使它们透明。
从您的问题应用于 MCVE,这将通过替换来完成:
tabShape.setVisible(item);
与:
tabShape.setOpacity(item ? 1.0 : 0.0);
在用户体验方面,您可以更进一步。您可以将它们设置为接近透明,而不是将“非 Activity ”星星设置为完全透明,就像在这个模型中一样(不透明度设置为 0.1
):
我看到的好处是:
关于java - 带有自定义 CellFactory 的 ListView 修剪不可见的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43016854/
我有一个 ReadOnlyBooleanProperty名为 caution对于每个 Trade我创建的对象。每个Trade对象填充 TableView 中的一行 fxTransactionLog .
我有一个奇怪的错误,我找不到合适的解决方案。我想向我的应用程序添加一个显示任务分类的 TreeTableView。由于用户可以自己添加任务,但这些任务必须与已经存在的任务区分开来,我希望它们在我的 T
我创建了一个单元工厂包装器来启用 JavaFX 表格单元的自定义配置。请参阅下面的代码。 package idmas.controller.modules.tableview; import idma
我对 JavaFX 中的 TreeView 有疑问。我不太明白使用细胞工厂的意义。 假设我有以下树: public void initialize(TreeView treeView){ tr
假设我有一个给定的 TableView 。我有一列 LocalDate键入一组cellFactory() 。 birthdayColumn.setCellFactory(column -> {
让我描述一下情况。 我有一个带有整数变量的类,我有一个包含 3 列“ 200”的 TableView,其中添加了 MyClass 对象。根据对象的 myIntValue,x 会显示在正确的列中。现在我
我正在尝试编写一种方法,允许我为作为参数传递的特定列设置列工厂。在这种情况下,我有订单和食品,这两个类在某个时候都显示在 TableView 中,并且都有一个列,我想将其格式化为价格。 它是这样工作的
TL;DR:监听器也在其他单元格上激活。 我有一个 TreeView,其中包含表示自定义类数据的不同 TreeItem。如果基础数据的 BooleanProperty 发生变化,则单元格应更改其颜色。
我有几个用于为约会调度程序应用程序选择日期和时间的ComboBox,但是当我自定义CellFactory时,它只能正确禁用正确的项目第一次单击并展开时。基本上,它应该禁用允许您选择 LocalDate
正如标题所示,我需要通过设置 CellFactory 将图像插入到 TableView 的列中但是通过FileChooser获取路径的图像没有显示在表格上。但是,如果我选择第一行,它会突出显示,就好像
我使用带有 CellFactory 的 ListView 为每个列表条目生成 Labels。当列表条目被选中时,无论我为list-view new ListCell () {
如何轻松地将自定义(可重用)cellFactories 应用到 javafx.scene.control.TableCell; 或 javafx.scene.control.TableView;? 最
我有: public abstract class V {} public class PV extends V {} public class TV extends V {} 我还有: ListVi
我正在使用包含 BillingTableRow 类型项目的 JavaFX TreeTableView。我希望根据 BillingTableRow 中的方法 getType() 给出的值对列进行样式设置
这是 this question 的后续内容... FXML 文件复制如下:
我有一个表列: TableColumn colStatus = new TableColumn("Status"); colStatus.setCellValueFactory(new Propert
我有一个小型概念验证应用程序,其中包含 6 个 Labels、一个 ComboBox 和一个 Button,所有这些都是使用 SceneBuilder 创建的。 通过单击按钮,应用程序会进行 Json
我有一个 ListView点击监听器。我想在悬停时添加新的监听器。我用 CellFactory 编码了那个新的监听器(下面的代码)。仅使用此代码我的 ListView ( ) 显示没有文本的项目,但
ageColumn.setCellFactory(param -> new TableCell(){ @Override protected void updateItem(Strin
看看下面的工作代码: class MyType{ SimpleStringProperty myname; SimpleObjectProperty mycolor
我是一名优秀的程序员,十分优秀!