如何在javafx的tableview中设置列宽? [英] How to set column width in tableview in javafx?

查看:57
本文介绍了如何在javafx的tableview中设置列宽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的表格.我应该将宽度设置为 30% 和 70%.该表是可扩展的,但不是列.我如何做到这一点?

I have a table with two columns. I should set the width to 30% and 70%. The table is expandable but not columns. How do I achieve that?

推荐答案

如果表格可扩展但列不可扩展",您的意思是用户不应该调整列的大小,然后调用 setResizable(false); 在每一列上.

If by "the table is expandable but not the columns", you mean the user should not be able to resize the columns, then call setResizable(false); on each column.

要使列保持相对于整个表格宽度的指定宽度,请绑定列的 prefWidth 属性.

To keep the columns at the specified width relative to the entire table width, bind the prefWidth property of the columns.

SSCCE:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableColumnResizePolicyTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Void> table = new TableView<>();
        TableColumn<Void, Void> col1 = new TableColumn<>("One");
        TableColumn<Void, Void> col2 = new TableColumn<>("Two");
        table.getColumns().add(col1);
        table.getColumns().add(col2);

        col1.prefWidthProperty().bind(table.widthProperty().multiply(0.3));
        col2.prefWidthProperty().bind(table.widthProperty().multiply(0.7));

        col1.setResizable(false);
        col2.setResizable(false);

        primaryStage.setScene(new Scene(new BorderPane(table), 600, 400));
        primaryStage.show();
    }

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

这篇关于如何在javafx的tableview中设置列宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆