- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用JavaFX,并尝试在树表中添加一个复选框项目,但看起来它只支持简单的树项目。
我的代码是Oracle's TreeTableView Example的修改版本:
public class TreeTableViewSample extends Application implements Runnable {
List<Employee> employees = Arrays.<Employee>asList(
new Employee("Ethan Williams", 30.0),
new Employee("Emma Jones", 10.0),
new Employee("Michael Brown", 70.0),
new Employee("Anna Black", 50.0),
new Employee("Rodger York", 20.0),
new Employee("Susan Collins", 70.0));
/* private final ImageView depIcon = new ImageView (
new Image(getClass().getResourceAsStream("department.png"))
);
*/
final CheckBoxTreeItem<Employee> root
= new CheckBoxTreeItem<>(new Employee("Sales Department", 0.0));
final CheckBoxTreeItem<Employee> root2
= new CheckBoxTreeItem<>(new Employee("Departments", 0.0));
public static void main(String[] args) {
Application.launch(TreeTableViewSample.class, args);
}
@Override
public void start(Stage stage) {
root.setExpanded(true);
employees.stream().forEach((employee) -> {
root.getChildren().add(new CheckBoxTreeItem<>(employee));
});
stage.setTitle("Tree Table View Sample");
final Scene scene = new Scene(new Group(), 400, 400);
scene.setFill(Color.LIGHTGRAY);
Group sceneRoot = (Group) scene.getRoot();
TreeTableColumn<Employee, String> empColumn
= new TreeTableColumn<>("Employee");
empColumn.setPrefWidth(150);
empColumn.setCellValueFactory(
(TreeTableColumn.CellDataFeatures<Employee, String> param)
-> new ReadOnlyStringWrapper(param.getValue().getValue().getName())
);
TreeTableColumn<Employee, Double> salaryColumn
= new TreeTableColumn<>("Salary");
salaryColumn.setPrefWidth(190);
/* salaryColumn.setCellValueFactory(
(TreeTableColumn.CellDataFeatures<Employee, String> param) ->
new ReadOnlyDoubleWrapper(param.getValue().getValue().getEmail())
);
*/
salaryColumn.setCellFactory(ProgressBarTreeTableCell.<Employee>forTreeTableColumn());
root2.getChildren().add(root);
TreeTableView<Employee> treeTableView = new TreeTableView<>(root2);
treeTableView.getColumns().setAll(empColumn, salaryColumn);
sceneRoot.getChildren().add(treeTableView);
stage.setScene(scene);
stage.show();
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
executorService.scheduleAtFixedRate(this, 3, 10, TimeUnit.SECONDS);
}
@Override
public void run() {
root2.getValue().setSalary(calcSalary(root));
}
public double calcSalary(TreeItem<Employee> t) {
Double salary = 0.0;
if (!t.isLeaf()) {
ObservableList<TreeItem<Employee>> al = t.getChildren();
for (int i = 0; i < al.size(); i++) {
TreeItem<Employee> get = al.get(i);
salary += calcSalary(get);
}
t.getValue().setSalary(salary);
}
return salary += t.getValue().getSalary();
}
public class Employee {
private SimpleStringProperty name;
private SimpleDoubleProperty salary;
public SimpleStringProperty nameProperty() {
if (name == null) {
name = new SimpleStringProperty(this, "name");
}
return name;
}
public SimpleDoubleProperty salaryProperty() {
if (salary == null) {
salary = new SimpleDoubleProperty(this, "salary");
}
return salary;
}
private Employee(String name, Double salary) {
this.name = new SimpleStringProperty(name);
this.salary = new SimpleDoubleProperty(salary);
}
public String getName() {
return name.get();
}
public void setName(String fName) {
name.set(fName);
}
public Double getSalary() {
return salary.get();
}
public void setSalary(Double fName) {
salary.set(fName);
}
}
}
有什么方法可以在上面的示例中使用树项目的复选框吗?我正在使用 JavaFx 8。
我还尝试创建工资条,它也可以用作任务及其子任务的进度条。 (只是玩 UI)。但不知道如何将它们与员工的真实值(value)观联系起来,因为我猜普通 TableView 与树 TableView 不同。谢谢 ! :)
最佳答案
没有与 CheckBoxTreeCell 相对应的单元格实现:这是一个带有绑定(bind)到 CheckBoxTreeItem 的 selected/indefinate 属性的复选框的单元格。明显的对应项 CheckBoxTreeTableCell 只是一个带有复选框的单元格,它绑定(bind)到单元格数据。
需要的是一个 CheckBoxTreeTableRow:这是可以访问 TreeItem 并可以管理 checkBox 和 treeItem 之间的绑定(bind)的单元格层。下面是 CheckBoxTreeCell 的快速实现、简化和调整副本。取消/绑定(bind)在 updateItem 中处理。
更新:干净的解决方案(很长!)
看起来TableRowSkinBase准备处理自定义行图形,它有一个方法graphicsProperty(),用于行皮肤内的所有布局代码。
/**
* Returns the graphic to draw on the inside of the disclosure node. Null
* is acceptable when no graphic should be shown. Commonly this is the
* graphic associated with a TreeItem (i.e. treeItem.getGraphic()), rather
* than a graphic associated with a cell.
*/
protected abstract ObjectProperty<Node> graphicProperty();
TreeTableRowSkin 实现它以返回 TreeItem 的图形,因此覆盖以返回 tableRow 的图形应该可以工作。除了......它不是 - 布局是弯曲的,如下面原始黑客答案中所述。挖掘暴露了罪魁祸首:它是 TreeTableCellSkin,它对自己的布局代码进行了硬编码,以解释其填充中的任何图形……treeItem 的图形。
所以需要一个完整的解决方案
第一对名为 DefaultTreeTableCell/Skin,下面的第二对名为 CheckBoxTreeTableRow/Skin。
用法(插入到 OP 示例中的片段)
// just for fun, have root items with some graphic
final CheckBoxTreeItem<Employee> root = new CheckBoxTreeItem<>(
new Employee("Sales Department", 0.0), new Circle(10, Color.RED));
final CheckBoxTreeItem<Employee> root2 = new CheckBoxTreeItem<>(
new Employee("Departments", 0.0), new Circle(10, Color.BLUE));
// configure treeTableView to use the extended tableRow
treeTableView.setRowFactory(item -> new CheckBoxTreeTableRow<>());
// configure table columns to use the extended table cell
empColumn.setCellFactory(p -> new DefaultTreeTableCell<>());
// all cell types must have a skin that copes with row graphics
salaryColumn.setCellFactory(e -> {
TreeTableCell cell = new ProgressBarTreeTableCell() {
@Override
protected Skin<?> createDefaultSkin() {
return new DefaultTreeTableCell.DefaultTreeTableCellSkin<>(this);
}
};
return cell;
});
单元格/行实现:
/**
* TreeTableCell actually showing something. This is copied from TreeTableColumn plus
* installs DefaultTreeTableCellSkin which handles row graphic width.
*/
public class DefaultTreeTableCell<S, T> extends TreeTableCell<S, T> {
@Override
protected void updateItem(T item, boolean empty) {
if (item == getItem()) return;
super.updateItem(item, empty);
if (item == null) {
super.setText(null);
super.setGraphic(null);
} else if (item instanceof Node) {
super.setText(null);
super.setGraphic((Node)item);
} else {
super.setText(item.toString());
super.setGraphic(null);
}
}
@Override
protected Skin<?> createDefaultSkin() {
return new DefaultTreeTableCellSkin<>(this);
}
/**
* TreeTableCellSkin that handles row graphic in its leftPadding, if
* it is in the treeColumn of the associated TreeTableView.
* <p>
* It assumes that per-row graphics - including the graphic of the TreeItem, if any -
* is folded into the TreeTableRow graphic and patches its leftLabelPadding
* to account for the graphic width.
* <p>
*
* Note: TableRowSkinBase seems to be designed to cope with variations of row
* graphic - it has a method <code>graphicProperty()</code> that's always used
* internally when calculating offsets in the treeColumn.
* Subclasses override as needed, the layout code remains constant. The real
* problem is the TreeTableCell hard-codes the TreeItem as the only graphic
* owner.
*
*/
public static class DefaultTreeTableCellSkin<S, T> extends TreeTableCellSkin<S, T> {
/**
* @param treeTableCell
*/
public DefaultTreeTableCellSkin(TreeTableCell<S, T> treeTableCell) {
super(treeTableCell);
}
/**
* Overridden to adjust the padding returned by super for row graphic.
*/
@Override
protected double leftLabelPadding() {
double padding = super.leftLabelPadding();
padding += getRowGraphicPatch();
return padding;
}
/**
* Returns the patch for leftPadding if the tableRow has a graphic of
* its own.<p>
*
* Note: this implemenation is a bit whacky as it relies on super's
* handling of treeItems graphics offset. A cleaner
* implementation would override leftLabelPadding from scratch.
* <p>
* PENDING JW: doooooo it!
*
* @return
*/
protected double getRowGraphicPatch() {
if (!isTreeColumn()) return 0;
Node graphic = getSkinnable().getTreeTableRow().getGraphic();
if (graphic != null) {
double height = getCellSize();
// start with row's graphic
double patch = graphic.prefWidth(height);
// correct for super's having added treeItem's graphic
TreeItem<S> item = getSkinnable().getTreeTableRow().getTreeItem();
if (item.getGraphic() != null) {
double correct = item.getGraphic().prefWidth(height);
patch -= correct;
}
return patch;
}
return 0;
}
/**
* Checks and returns whether our cell is attached to a treeTableView/column
* and actually has a TreeItem.
* @return
*/
protected boolean isTreeColumn() {
if (getSkinnable().isEmpty()) return false;
TreeTableColumn<S, T> column = getSkinnable().getTableColumn();
TreeTableView<S> view = getSkinnable().getTreeTableView();
if (column.equals(view.getTreeColumn())) return true;
return view.getVisibleLeafColumns().indexOf(column) == 0;
}
}
}
/**
* Support custom graphic for Tree/TableRow. Here in particular a checkBox.
* http://stackoverflow.com/q/29300551/203657
* <p>
* Basic idea: implement custom TreeTableRow that set's its graphic to the
* graphic/checkBox. Doesn't work: layout is broken, graphic appears
* over the text. All fine if we set the graphic to the TreeItem that's
* shown. Possible as long as the treeItem doesn't have a graphic of
* its own.
* <p>
* Basic problem:
* <li> TableRowSkinBase seems to be able to cope: has protected method
* graphicsProperty that should be implemented to return the graphic
* if any. That graphic is added to the children list and sized/located
* in layoutChildren.
* <li> are added the graphic/disclosureNode as needed before
* calling super.layoutChildren,
* <li> graphic/disclosure are placed inside the leftPadding of the tableCell
* that is the treeColumn
* <li> TreeTableCellSkin must cooperate in taking into account the graphic/disclosure
* when calculating its leftPadding
* <li> cellSkin is hard-coded to use the TreeItem's graphic (vs. the rowCell's)
*
* PENDING JW:
* <li>- would expect to not alter the scenegraph during layout (might lead to
* endless loops or not) but done frequently in core code
* <p>
*
* Outline of the solution as implemented:
* <li> need a TreeTableCell with a custom skin
* <li> override leftPadding in skin to add row graphic if available
* <li> need CheckBoxTreeTableRow that sets its graphic to checkBox (or a combination
* of checkBox and treeItem's)
* <li> need custom rowSkin that implements graphicProperty to return the row graphic
*
* @author Jeanette Winzenburg, Berlin
*
* @see DefaultTreeTableCell
* @see DefaultTreeTableCellSkin
*
*/
public class CheckBoxTreeTableRow<T> extends TreeTableRow<T> {
private CheckBox checkBox;
private ObservableValue<Boolean> booleanProperty;
private BooleanProperty indeterminateProperty;
public CheckBoxTreeTableRow() {
this(item -> {
if (item instanceof CheckBoxTreeItem<?>) {
return ((CheckBoxTreeItem<?>)item).selectedProperty();
}
return null;
});
}
public CheckBoxTreeTableRow(
final Callback<TreeItem<T>, ObservableValue<Boolean>> getSelectedProperty) {
this.getStyleClass().add("check-box-tree-cell");
setSelectedStateCallback(getSelectedProperty);
checkBox = new CheckBox();
checkBox.setAlignment(Pos.TOP_LEFT);
}
// --- selected state callback property
private ObjectProperty<Callback<TreeItem<T>, ObservableValue<Boolean>>>
selectedStateCallback =
new SimpleObjectProperty<Callback<TreeItem<T>, ObservableValue<Boolean>>>(
this, "selectedStateCallback");
/**
* Property representing the {@link Callback} that is bound to by the
* CheckBox shown on screen.
*/
public final ObjectProperty<Callback<TreeItem<T>, ObservableValue<Boolean>>> selectedStateCallbackProperty() {
return selectedStateCallback;
}
/**
* Sets the {@link Callback} that is bound to by the CheckBox shown on screen.
*/
public final void setSelectedStateCallback(Callback<TreeItem<T>, ObservableValue<Boolean>> value) {
selectedStateCallbackProperty().set(value);
}
/**
* Returns the {@link Callback} that is bound to by the CheckBox shown on screen.
*/
public final Callback<TreeItem<T>, ObservableValue<Boolean>> getSelectedStateCallback() {
return selectedStateCallbackProperty().get();
}
/** {@inheritDoc} */
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
TreeItem<T> treeItem = getTreeItem();
checkBox.setGraphic(treeItem == null ? null : treeItem.getGraphic());
setGraphic(checkBox);
// uninstall bindings
if (booleanProperty != null) {
checkBox.selectedProperty().unbindBidirectional((BooleanProperty)booleanProperty);
}
if (indeterminateProperty != null) {
checkBox.indeterminateProperty().unbindBidirectional(indeterminateProperty);
}
// install new bindings.
// this can only handle TreeItems of type CheckBoxTreeItem
if (treeItem instanceof CheckBoxTreeItem) {
CheckBoxTreeItem<T> cbti = (CheckBoxTreeItem<T>) treeItem;
booleanProperty = cbti.selectedProperty();
checkBox.selectedProperty().bindBidirectional((BooleanProperty)booleanProperty);
indeterminateProperty = cbti.indeterminateProperty();
checkBox.indeterminateProperty().bindBidirectional(indeterminateProperty);
} else {
throw new IllegalStateException("item must be CheckBoxTreeItem");
}
}
}
@Override
protected Skin<?> createDefaultSkin() {
return new CheckBoxTreeTableRowSkin<>(this);
}
public static class CheckBoxTreeTableRowSkin<S> extends TreeTableRowSkin<S> {
protected ObjectProperty<Node> checkGraphic;
/**
* @param control
*/
public CheckBoxTreeTableRowSkin(TreeTableRow<S> control) {
super(control);
}
/**
* Note: this is implicitly called from the constructor of LabeledSkinBase.
* At that time, checkGraphic is not yet instantiated. So we do it here,
* still having to create it at least twice. That'll be a problem if
* anybody would listen to changes ...
*/
@Override
protected ObjectProperty<Node> graphicProperty() {
if (checkGraphic == null) {
checkGraphic = new SimpleObjectProperty<Node>(this, "checkGraphic");
}
CheckBoxTreeTableRow<S> treeTableRow = getTableRow();
if (treeTableRow.getTreeItem() == null) {
checkGraphic.set(null);
} else {
checkGraphic.set(treeTableRow.getGraphic());
}
return checkGraphic;
}
protected CheckBoxTreeTableRow<S> getTableRow() {
return (CheckBoxTreeTableRow<S>) super.getSkinnable();
}
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(CheckBoxTreeTableRow.class.getName());
}
<小时/>原始答案:hack!
里面有一行疯狂的代码:
treeItem.setGraphics(checkBox);
这真的很奇怪,最终可能会造成严重破坏 - 这是围绕 TreeTableRowSkin 中布局故障的黑客攻击,由于某种原因(我无法挖掘)无法将图形集定位到单元格。无法使其在自定义 CheckBoxTreeTableRowSkin 中正常运行,直接在其 graphicProperty()
中返回复选框 - 因此我们现在就进行破解。
/**
* @author Jeanette Winzenburg, Berlin
*/
public class CheckBoxTreeTableRowHack<T> extends TreeTableRow<T> {
private CheckBox checkBox;
private ObservableValue<Boolean> booleanProperty;
private BooleanProperty indeterminateProperty;
public CheckBoxTreeTableRowHack() {
setSelectedStateCallback(item -> {
if (item instanceof CheckBoxTreeItem<?>) {
return ((CheckBoxTreeItem<?>)item).selectedProperty();
}
return null;
});
this.checkBox = new CheckBox();
// something weird going on with layout
checkBox.setAlignment(Pos.TOP_LEFT);
}
// --- selected state callback property
private ObjectProperty<Callback<TreeItem<T>, ObservableValue<Boolean>>>
selectedStateCallback =
new SimpleObjectProperty<Callback<TreeItem<T>, ObservableValue<Boolean>>>(
this, "selectedStateCallback");
/**
* Property representing the {@link Callback} that is bound to by the
* CheckBox shown on screen.
*/
public final ObjectProperty<Callback<TreeItem<T>, ObservableValue<Boolean>>> selectedStateCallbackProperty() {
return selectedStateCallback;
}
/**
* Sets the {@link Callback} that is bound to by the CheckBox shown on screen.
*/
public final void setSelectedStateCallback(Callback<TreeItem<T>, ObservableValue<Boolean>> value) {
selectedStateCallbackProperty().set(value);
}
/**
* Returns the {@link Callback} that is bound to by the CheckBox shown on screen.
*/
public final Callback<TreeItem<T>, ObservableValue<Boolean>> getSelectedStateCallback() {
return selectedStateCallbackProperty().get();
}
/** {@inheritDoc} */
@Override
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
//
TreeItem<T> treeItem = getTreeItem();
// PENDING JW: this is nuts but working .. certainly will pose problems
// when re-using the cell
treeItem.setGraphic(checkBox);
// this is what CheckBoxTreeCell does, setting the graphic
// of the tableRow confuses the layout
// checkBox.setGraphic(treeItem == null ? null : treeItem.getGraphic());
// setGraphic(checkBox);
// uninstall bindings
if (booleanProperty != null) {
checkBox.selectedProperty().unbindBidirectional((BooleanProperty)booleanProperty);
}
if (indeterminateProperty != null) {
checkBox.indeterminateProperty().unbindBidirectional(indeterminateProperty);
}
// install new bindings.
// We special case things when the TreeItem is a CheckBoxTreeItem
if (treeItem instanceof CheckBoxTreeItem) {
CheckBoxTreeItem<T> cbti = (CheckBoxTreeItem<T>) treeItem;
booleanProperty = cbti.selectedProperty();
checkBox.selectedProperty().bindBidirectional((BooleanProperty)booleanProperty);
indeterminateProperty = cbti.indeterminateProperty();
checkBox.indeterminateProperty().bindBidirectional(indeterminateProperty);
} else {
throw new IllegalStateException("item must be CheckBoxTreeItem");
}
}
}
}
// usage: in the example add
treeTableView.setRowFactory(f -> new CheckBoxTreeTableRowHack<>());
关于JavaFX : Add CheckBoxTreeItem in TreeTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29300551/
我不知道这是不是问这种问题的最佳地点, 我看到一些 JavaFX伙计们,重新标记 一些问题通过替换 javafx来自 javafx-2并采用新的 javafx-8 .它被弃用了还是什么? 编辑 : 不
错误本身: Error:java: invalid flag: --add-modules=javafx.fxml,javafx.graphics,javafx.controls,javafx.bas
这个想法是让一个应用程序在每个显示器上显示两个不同的窗口(阶段),该应用程序应该知道计算机有多少个显示器及其分辨率。 javafx有可能吗? 最佳答案 对于当前版本的 JavaFX (2.2),您可以
我正在将我的项目从 javafx 1.3 转换为 javafx 2.1。但我对 javafx.lang 有疑问包裹。 最佳答案 JavaFX 1.3 lang 包内容被拆分并移至下一个位置: 时长变为
当我尝试将标签添加到 gridpane 中时,如第二张图片所示,它不起作用。我已经尝试了很多东西,比如添加 CSS,但它仍然无法正常工作。为什么第 113 和 114 行不起作用? (opcje.se
我有一个JavaFX ContextMenu分配给滚动面板的鼠标右键单击。它会打开,但在滚动 Pane 外部单击时不会关闭。我可以在滚动 Pane 中添加另一个鼠标事件以将其隐藏,但这只能解决1个问题
我有一个tableview,其中附有一个可观察到的自定义类对象的列表(类类型:SalesInvoiceNetSale)。该表中的所有数据都可以正常显示。可观察列表中的最后一项是总计行(类类型:Sale
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我想知道如何在JavaFX中绘制半圆。我尝试使用Shape和QuadCurve,但无法制作出完美的半圆。 这是我要绘制的图片: 最佳答案 您链接的图片实际上是一个半圆环。您可以通过绘制嵌套的2条圆弧和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在寻找 JavaFX 支持的图像类型(最新)列表,例如PNG、JPEG、TIFF。不同的搜索引擎没有帮助......知道从哪里开始吗? 更特别的是,我对 16 位灰度图像(不同格式)和罕见的受支持
我希望在 javafx 中让标签每 0.1 秒闪烁一次。文本显示在后台运行的 ImageView gif 的顶部。我将如何去做,或者您对最佳方法有什么建议? 谢谢 最佳答案 @fabian 的解决方案
我需要测试所选项目的值以调用不同的方法,因此我编写了添加侦听器的代码,但是该代码生成语法错误 @FXML private JFXComboBox cmbComp; cmbComp.valuePrope
我正在 Javafx 中编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击按钮时,我可以使用另一个按钮加载另一个场景和舞台上的一个文本框,然后删除
编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗? 请注意,“所有
我想使用 JavaFX 编写一个笔画绘图应用程序。我有一个压敏绘图板,如果能够读取笔的压力和倾斜值,那就太好了。 JavaFX 有一个 API 可以处理鼠标、触摸和滑动输入,但似乎没有任何东西可以产生
我在 JavaFX 中使用条形图和折线图。当我使两个图表大小相同并将它们放在同一位置时,它们完美地相互重叠。我如何使折线图显示在条形图的顶部。 目前我已将它们的不透明度设置为 0.7,这样它们“看起来
此问题与 this 相关。现在我想为字段值等于某个值的行着色。 @FXML private TableView tv_mm_view; @FXML private Ta
我有一个程序,可以生成高度图(0-255 的整数的 2D 数组),并使用 Shape3D“Box”对象为每个“像素”构建 3D View ,其高度与其在高度图中的值成比例。这会创建一个看起来很酷的四四
我想为 JavaFX 创建上下文菜单。这是我测试过的代码。但是由于某种原因,当我右键单击树节点时没有上下文菜单。你能帮我找出我的错误吗。 import java.util.Arrays; import
我是一名优秀的程序员,十分优秀!