java - 如何在表构造的所有列之间分配总宽度?”

标签 java javafx tableview javafx-tableview

我正在尝试编写一个自定义调整大小策略,其行为类似于 TableView.CONSTRAINED_RESIZE_POLICY因为它将所有可见列的总宽度设置为表中的总可用宽度,以便水平滚动条永远不会出现。

我正在使用线路double widthAvailable = table.getWidth() - getScrollbarWidth(table);尝试这样做(请参阅下面的完整代码)。

不幸的是,这个计算似乎返回 4太多。我的猜测是,我还应该从我缺少的表宽度中减去其他一些东西,在默认的 JavaFX 主题中,它恰好是 4 像素宽。

这是演示该问题的完整程序:

package net.joshuad.hypnos.test;

import java.util.List;
import java.util.Locale;
import java.util.Set;

import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class CustomResizeExample extends Application {

    @SuppressWarnings("unchecked")
    private Parent getContent () {
        TableView <Locale> table = new TableView <>( FXCollections.observableArrayList( Locale.getAvailableLocales() ) );
        TableColumn <Locale, String> countryCode = new TableColumn <>( "CountryCode" );
        countryCode.setCellValueFactory( new PropertyValueFactory <>( "country" ) );
        TableColumn <Locale, String> language = new TableColumn <>( "Language" );
        language.setCellValueFactory( new PropertyValueFactory <>( "language" ) );
        table.getColumns().addAll( countryCode, language );

        TableColumn <Locale, Locale> local = new TableColumn <>( "Locale" );
        local.setCellValueFactory( c -> new SimpleObjectProperty <>( c.getValue() ) );

        table.getColumns().addAll( local );
        table.setColumnResizePolicy( new CustomResizePolicy() );

        BorderPane pane = new BorderPane( table );
        return pane;
    }
    @Override
    public void start ( Stage stage ) throws Exception {
        stage.setScene( new Scene( getContent(), 800, 400 ) );
        stage.show();
    }

    public static void main ( String[] args ) {
        launch ( args );
    }

}

@SuppressWarnings ( "rawtypes" ) 
class CustomResizePolicy implements Callback <TableView.ResizeFeatures, Boolean> {

    @SuppressWarnings("unchecked")
    @Override
    public Boolean call ( TableView.ResizeFeatures feature ) {

        TableView table = feature.getTable();
        List <TableColumn> columns = table.getVisibleLeafColumns();
        double widthAvailable = table.getWidth() - getScrollbarWidth ( table );


        double forEachColumn = widthAvailable / columns.size();

        for ( TableColumn column : columns ) {
            column.setMinWidth( forEachColumn );
            column.setMaxWidth( forEachColumn );
        }

        return true;

    }

    private double getScrollbarWidth ( TableView table ) {
        double scrollBarWidth = 0;
        Set <Node> nodes = table.lookupAll( ".scroll-bar" );
        for ( final Node node : nodes ) {
            if ( node instanceof ScrollBar ) {
                ScrollBar sb = (ScrollBar) node;
                if ( sb.getOrientation() == Orientation.VERTICAL ) {
                    if ( sb.isVisible() ) {
                        scrollBarWidth = sb.getWidth();
                    }
                }
            }
        }

        return scrollBarWidth;
    }
}

看到 table 比应有的宽一点了吗?我希望能够设置列的宽度,以便不出现水平滚动条。

enter image description here

最佳答案

Scenic View是一个熟悉的有用工具。它为您提供了有关场景图的许多详细信息。您正在寻找的空间是clipped-container:

enter image description here

enter image description here

请注意,按照您的有效方式设置 CustomResizePolicy 不允许调整列的大小,因为它们始终分配相同份额的可用空间。

以下是您可能正在寻找的计算结果:

Region region = null;
Set<Node> nodes = table.lookupAll(".clipped-container");
for (Node node : nodes) {
    if (node instanceof Region) {
        region = (Region) node;
    }
}
for (TableColumn<Locale, ?> column : table.getColumns()) {
    column.setPrefWidth(region.getWidth() / table.getColumns().size());
}

我不完全明白为什么CONSTRAINED_RESIZE_POLICY不是你想要的,因为它平均划分空间并且不允许调整大小超过分配的空间(因此不会出现水平滚动条),但如果您正在制定一些特殊的调整大小策略,上述内容应该会有所帮助。

关于java - 如何在表构造的所有列之间分配总宽度?”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47852175/

相关文章:

java - 当显示 HTML 文本时,禁用时 JLabel 不会变灰

java - 有没有办法获取 JavaFX 属性的所有绑定(bind)?

java - 在一个窗口中制作多个按钮。 JavaFX

swift - 如何从 MPMediaItemCollection 中提取艺术家值(value)

iphone - TabBarController + TableViewController + 导航 Controller ?

java - Comparator.comparing 与 lambda 的两个参数

Java 8 流 : Read file word by word

java - JSP:不评估 EL 表达式

javafx - 如何删除javafx折线图中的图例

JavaFX:如何 "repaint"一个 TableView