如何在javafx中设置tableview中的列宽? [英] How to set column width in tableview in javafx?
本文介绍了如何在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屋!
查看全文