sak*_*kit 5 java jxtreetable property-binding javafx-8 treetableview
我制作了一个简单的JavaFX应用程序。在此应用程序中,有一个2列的树形表和一个复选框。如果选中此复选框,则第2列将可见,否则不可见。为此,我将树形表的列可见属性绑定到复选框selected属性。当我单击复选框时,列状态会发生变化,但同时会给出。
原因:java.lang.RuntimeException:TreeTableColumn.visible:无法设置绑定值。
如果我使用双向绑定,则不会出现此错误。但是我不需要双向绑定。是错误还是我没有正确使用绑定?请使用下面的代码重现此错误。我使用jdk1.8.0_111。
JavaFXApplication.java
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication 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();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
FXMLDocumentController.java
package test;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
public class FXMLDocumentController implements Initializable {
@FXML
private TreeTableView<?> table;
@FXML
private CheckBox box;
@FXML
private TreeTableColumn<?, ?> c2;
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
c2.visibleProperty().bind(box.selectedProperty());
}
}
Run Code Online (Sandbox Code Playgroud)
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.TreeTableColumn?>
<?import javafx.scene.control.TreeTableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="390.0" prefWidth="452.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65" fx:controller="javafxapplication2.FXMLDocumentController">
<children>
<TreeTableView fx:id="table" layoutX="2.0" prefHeight="390.0" prefWidth="149.0">
<columns>
<TreeTableColumn prefWidth="75.0" text="C1" />
<TreeTableColumn fx:id="c2" prefWidth="75.0" text="C2" visible="false" />
</columns>
</TreeTableView>
<CheckBox fx:id="box" layoutX="234.0" layoutY="77.0" mnemonicParsing="false" text="CheckBox" />
</children>
</AnchorPane>
Run Code Online (Sandbox Code Playgroud)
我认为这实际上不是一个错误。但这绝对是神秘且意想不到的行为。部分问题在于绑定冲突来自TableHeaderRow,它是由外观在显示时创建的。
负责TableHeaderRow呈现所有列标题,并且它包含一个菜单的内置按钮,默认情况下,该按钮是要显示/隐藏的列的单选列表:
结果,TableHeaderRow在这些菜单项的选择状态和每列的可见属性之间创建了双向绑定。
实际上可以撤消此绑定,但我发现它很烦人,因为TableHeaderRow直到显示null为止TableView。但是,调整此解决方案来访问 TableHeaderRow,我们可以执行以下操作:
TableHeaderRow headerRow = ((TableViewSkinBase) tableView.getSkin()).getTableHeaderRow();
try {
// get columnPopupMenu field
Field privateContextMenuField = TableHeaderRow.class.getDeclaredField("columnPopupMenu");
// make field public
privateContextMenuField.setAccessible(true);
// get context menu
ContextMenu contextMenu = (ContextMenu) privateContextMenuField.get(headerRow);
for (MenuItem menuItem : contextMenu.getItems()) {
// Assuming these will be CheckMenuItems in the default implementation
BooleanProperty selectedProperty = ((CheckMenuItem) menuItem).selectedProperty();
// In theory these menu items are in parallel with the columns, but I just brute forced it to test
for (TableColumn<?, ?> tableColumn : tableView.getColumns()) {
// Unlink the column's visibility with the menu item
tableColumn.visibleProperty().unbindBidirectional(selectedProperty);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
// Not strictly necessary but we don't want to be misleading :-p
tableView.setTableMenuButtonVisible(false);
Run Code Online (Sandbox Code Playgroud)
但这里有一个关键点:每次列的可见性发生变化时,您都必须执行此操作,因为TableHeaderRow每次显示列时,都有一个重建菜单并重新链接属性的侦听器。
当然,您可以找到一种方法来禁用该侦听器..但显然这已经很荒谬并且可能没有必要。
无论如何,正如您所指出的:
如果我使用双向绑定,我不会收到此错误。但我不需要双向绑定。
我认为第二个陈述在技术上并不正确:您的用例不需要双向绑定,但它实际上在这里是合适的,因为还有其他属性已经与列的可见性链接。
所以,我会使用双向绑定。