JavaFX 11 替换 column.impl_setWidth()?

Jos*_*uaD 5 java javafx java-11 javafx-11

我写了一个自定义的 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试 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 );
}
Run Code Online (Sandbox Code Playgroud)

尝试 3:

查看 JavaFX 的来源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();
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试 4:

我还尝试使用此答案中提供的解决方案来执行私有方法,但遇到了相同的异常——“找不到名称为 doSetWidth 和 params [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();
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Jos*_*uaD 3

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

--add-opens javafx.controls/javafx.scene.control=ALL-UNNAMED
Run Code Online (Sandbox Code Playgroud)

  • 另请注意,“com.sun.javafx.scene.control.TableColumnBaseHelper.setWidth(TableColumnBase, double)”可能在不使用反射的情况下工作,但您仍然必须使用私有 API。更新:看起来它就是为此目的而设计的。不过是偶然发现的。 (2认同)
  • 嗯,“com.sun....”中的内容被视为私有 API。它应该生成一个警告,您需要在编译时抑制该警告。我仍在使用 Java8/JavaFX8,所以我不确定 OpenJFX11 是否仍然如此。 (2认同)