首页 > 解决方案 > JavaFX 11 替换 column.impl_setWidth()?

问题描述

我写了一个自定义的 TableColumn 宽度调整策略。你可以在 github 上看到它在 Java 8 / JavaFX 8 下的代码。如您所见,在方法distributeSpaceRatio()和其他地方,取决于TableColumn.impl_setWidth()在进行计算后将列的宽度设置为所需的值。

我正在将我的项目迁移到 Java 11 / JavaFX 11,并且该方法impl_setWidth()已从公共视图中删除。

是否有另一种方法来告诉表格列设置其宽度?我对任何解决方案持开放态度,只要它是可靠的并将 with 设置为(或接近)请求的值。

以下是我迄今为止尝试过的两个无效的想法:


尝试1:

这给出了 NoMethodFoundException:

private void setColumnWidth ( TableColumn column, double targetWidth ) {

    try {
        Method method = column.getClass().getDeclaredMethod( "setWidth" );
        method.setAccessible( true );
        Object r = method.invoke( targetWidth );
    } catch ( Exception e ) {
        e.printStackTrace();
    }
}

尝试2:

这个没有效果:

private void setColumnWidth ( TableColumn column, double targetWidth ) {

    double oldMax = column.getMaxWidth();
    double oldMin = column.getMinWidth();
    double oldPref = column.getPrefWidth();

    column.setMaxWidth( targetWidth );
    column.setMinWidth( targetWidth );
    column.setPrefWidth( targetWidth );

    column.getTableView().refresh();

    column.setPrefWidth( oldPref );
    column.setMinWidth( oldMin );
    column.setMaxWidth( oldMax );
}

尝试 3:

查看 JavaFX's 的源代码TableView.UNCONSTRAINED_RESIZE_POLICY,似乎调用的方法最终是TableColumnBase.doSetWidth(),但这对我来说也不起作用:

private void setColumnWidth ( TableColumn column, double targetWidth ) {

    try {
        Method method = column.getClass().getDeclaredMethod( "doSetWidth" );
        method.setAccessible( true );
        Object r = method.invoke( targetWidth );
    } catch ( Exception e ) {
        e.printStackTrace();
    }
}

尝试4:

我还尝试使用此答案中提供的解决方案来执行私有方法,并得到相同的异常 - “没有找到名称为 doSetWidth 和参数 [class java.lang.Double] 的方法”。


尝试 5:

我以为这个会起作用,但没有这样的运气。它抛出了“java.lang.NoSuchFieldException:width”。但是,当我转到 TableColumnBase 源代码时,我在第 412 行清楚地看到了一个名为 width 的私有字段。不知道为什么这会失败。

private void setColumnWidth ( TableColumnBase column, double targetWidth ) {
    try {
        Field widthField = column.getClass().getDeclaredField( "width" );
        widthField.setAccessible( true );
        ReadOnlyDoubleWrapper width = (ReadOnlyDoubleWrapper)widthField.get( column );
        width.set( targetWidth );
    } catch ( Exception e ) {
        e.printStackTrace();
    }
}

尝试 6:

这让我们更接近,但仍然失败:

private void setColumnWidth ( TableColumnBase column, double targetWidth ) {
    try {
        Field widthField = column.getClass().getSuperclass().getDeclaredField( "width" );
        widthField.setAccessible( true );
        ReadOnlyDoubleWrapper width = (ReadOnlyDoubleWrapper)widthField.get( column );
        width.set( targetWidth );
    } catch ( Exception e ) {
        e.printStackTrace();
    }
}

java.lang.reflect.InaccessibleObjectException:无法将字段设为私有 javafx.beans.property.ReadOnlyDoubleWrapper javafx.scene.control.TableColumnBase.width 可访问:模块 javafx.controls 不会“打开 javafx.scene.control”到未命名的模块 @649dcffc


尝试 7:

private void setColumnWidth ( TableColumn column, double targetWidth ) {
    try {
        Method method = column.getClass().getSuperclass().getDeclaredMethod( "setWidth", double.class );
        method.setAccessible( true );
        method.invoke ( targetWidth );
        //ReadOnlyDoubleWrapper width = (ReadOnlyDoubleWrapper)widthField.get( column );
        //width.set( targetWidth );
    } catch ( Exception e ) {
        e.printStackTrace();
    }
}

java.lang.reflect.InaccessibleObjectException:无法使 void javafx.scene.control.TableColumnBase.setWidth(double) 可访问:模块 javafx.controls 不会“打开 javafx.scene.control”到未命名的模块 @5063f647

标签: javajavafxjava-11javafx-11

解决方案


基于测试和其他人的帮助,我找到了这个解决方案:

private void setColumnWidth ( TableColumn column, double targetWidth ) {
    try {
        Method method = TableColumnBase.class.getDeclaredMethod( "doSetWidth", double.class );
        method.setAccessible( true );
        method.invoke( column, targetWidth );
    } catch ( NoSuchMethodException | InvocationTargetException | IllegalAccessException e ) {
        e.printStackTrace();
    }
}

此外,需要使用以下参数调用 JVM:

--add-opens javafx.controls/javafx.scene.control=ALL-UNNAMED

推荐阅读