如何在桌子构造的所有列中扩展总宽度?“

问题描述 投票:2回答:1

我正在尝试编写一个自定义调整大小策略,其作用类似于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;
    }
}

看看桌子的宽度是多少?我希望能够设置列的宽度,以便不显示水平滚动条。

enter image description here

java javafx tableview javafx-tableview
1个回答
2
投票

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不是你想要的,因为它平均划分空间并且不允许调整大小超过分配的空间(因此不会出现水平滚动条),但是如果你正在做一些特殊的调整大小政策以上应该有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.