代码之家  ›  专栏  ›  技术社区  ›  Sunflame

javafx:tableviewskin初始化

  •  1
  • Sunflame  · 技术社区  · 6 年前

    我正在执行自定义 TableView 自定义等元素 ContextMenu 作为表格菜单。为了达到这个目的我需要一个 TableHeaderRow 但我这里有问题。在 initialize() 方法 TableView#getSkin() 为空。但我想知道为什么?什么时候 Skin 初始化?为了得到一个暂时的解决方案 Platform.runLater() 但并不是每次都像预期的那样奏效。我记得@kleopatra在评论中提到,你不能确定 runLater() 在预期时间执行。

    这有时会导致自定义上下文菜单无法正确呈现。

    这就是我 Controller 班级看起来像:

    public class Controller implements Initializable {
    
        @FXML
        private TableView<?> tableView;
    
        @Override
        public void initialize(URL location, ResourceBundle resources) {
    
            // null
            System.out.println(tableView.getSkin());
    
            // most of the time not null. but it happens to be null
            Platform.runLater(() -> System.out.println(tableView.getSkin()));
    
            // here getTableHeader returns null
            applyCustomTableMenu(getTableHeader(tableView));
    
            // here getTableHeader sometimes returns the nonnull reference
            Platform.runLater(() -> applyCustomTableMenu(getTableHeader(tableView)));
        }
    
        private TableHeaderRow getTableHeader(TableView<?> tableView) {
            return (TableHeaderRow) tableView.queryAccessibleAttribute(AccessibleAttribute.HEADER);
        }
    
        private void applyCustomTableMenu(TableHeaderRow tableHeaderRow) {
            // implementation
            System.out.println(tableHeaderRow);
        }
    
    }
    

    当我可以确定皮肤已经初始化并且可以获得对我的 台头铁 是吗?

    我也会接受任何建议,每次都会给我一个非空的参考。 台头铁 是的。

    0 回复  |  直到 6 年前