TreeTableColumn.visible: the bound value cannot be set

I am making a simple JavaFX application. This app has a three-element column and check box. If checked, column 2 will be visible, otherwise it will not be visible. To do this, I linked the visibility of the column property of the table tree to the selected property. When I click on the state of the checkbox column, but at the same time gives.

Raised: java.lang.RuntimeException: TreeTableColumn.visible: A bound value could not be set.

If I use bi-directional binding, I do not get this error. But I do not need bidirectional binding. Is this a bug, or am I using bindings incorrectly? Please use the code below to reproduce this error. I am using 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);
    }

}

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());
    }    

}

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>
+4
1

, . , . , TableHeaderRow, .

TableHeaderRow , / :

TableView with column header button

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 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);

: , , TableHeaderRow , , , .

, ... , , , , .

, :

, . .

, : , , , .

, .

0

Source: https://habr.com/ru/post/1660896/


All Articles