JavaFX:设置每列的最小宽度
我试图将每列的最小宽度设置为100px。我更喜欢使用JavaFX:设置每列的最小宽度,java,javafx,tableview,Java,Javafx,Tableview,我试图将每列的最小宽度设置为100px。我更喜欢使用tableView.setColumnResizePolicy(tableView.constrated\u RESIZE\u策略) 解释 如果设置TableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_策略),则所有列的大小将相等,直到达到TableView的最大宽度 此时,每列的宽度由表视图的宽度除以列数决定 我试图将每一列设置为最小宽度,但没有成功。我还看到人们刚刚为se
tableView.setColumnResizePolicy(tableView.constrated\u RESIZE\u策略)代码>
解释
如果设置TableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_策略),则所有列的大小将相等,直到达到TableView的最大宽度
此时,每列的宽度由表视图的宽度除以列数决定
我试图将每一列设置为最小宽度,但没有成功。我还看到人们刚刚为setColumnResizePolicy
创建了自己的回调,但我无法实现我的想法
MCVE
public class MCVE extends Application {
private Scene mainScene;
private Stage mainStage;
private Label filename;
private VBox mainBox;
private TableView<String> tableView;
private Button open;
private Button save;
private Button neu;
private Button settings;
private Button table;
private Button row;
private Button column;
private Button date;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
initButton();
initTable();
mainStage = new Stage();
filename = new Label("Nr. 100 - Test Data (Applikation: TEST)");
mainScene = new Scene(mainVBox(), 1200, 600);
tableView.prefWidthProperty().bind(mainBox.widthProperty());
tableView.prefHeightProperty().bind(mainBox.heightProperty());
mainStage.setScene(mainScene);
mainStage.show();
}
private GridPane mainGrid() {
GridPane gridPane = new GridPane();
gridPane.setVgap(10);
gridPane.setHgap(10);
gridPane.add(filename, 0, 0);
gridPane.add(buttonBox(), 0, 1);
gridPane.add(tableView, 0, 2);
return gridPane;
}
private VBox buttonBox() {
VBox buttonBox = new VBox();
HBox firstRowBox = new HBox();
HBox secRowBox = new HBox();
firstRowBox.getChildren().addAll(open, save, neu, settings);
firstRowBox.setSpacing(5);
secRowBox.getChildren().addAll(table, row, column, date);
secRowBox.setSpacing(5);
buttonBox.getChildren().addAll(firstRowBox, secRowBox);
buttonBox.setSpacing(5);
buttonBox.prefWidthProperty().bind(mainBox.widthProperty());
return buttonBox;
}
private VBox mainVBox() {
mainBox = new VBox();
mainBox.prefWidthProperty().bind(mainStage.widthProperty().multiply(0.8));
mainBox.setPadding(new Insets(10, 10, 10 ,10));
mainBox.getChildren().add(mainGrid());
return mainBox;
}
private void initButton() {
open = new Button("Open");
open.setPrefWidth(100);
save = new Button("Save");
save.setPrefWidth(100);
neu = new Button("New");
neu.setPrefWidth(100);
settings = new Button("Settings");
settings.setPrefWidth(100);
table = new Button("Table");
table.setPrefWidth(100);
row = new Button("Row");
row.setPrefWidth(100);
column = new Button("Column");
column.setPrefWidth(100);
date = new Button("Date");
date.setPrefWidth(100);
}
private TableView initTable() {
tableView = new TableView<>();
// Create column UserName (Data type of String).
TableColumn<String, String> userNameCol //
= new TableColumn<>("User Name");
// Create column Email (Data type of String).
TableColumn<String, String> emailCol//
= new TableColumn<>("Email");
// Create 2 sub column for FullName.
TableColumn<String, String> firstNameCol //
= new TableColumn<>("First Name");
TableColumn<String, String> lastNameCol //
= new TableColumn<>("Last Name");
// Active Column
TableColumn<String, Boolean> activeCol//
= new TableColumn<>("Active");
tableView.getColumns().addAll(userNameCol, emailCol, firstNameCol, lastNameCol, activeCol);
for (int i = 0; tableView.getColumns().size() < i; i++){
tableView.getColumns().get(i).setMinWidth(100);
}
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
return tableView;
}
}
公共类MCVE扩展应用程序{
私密场景;
私人舞台主体;
私有标签文件名;
专用VBox主机箱;
私有TableView TableView;
私人按钮打开;
私人按钮保存;
专用按钮neu;
私人按钮设置;
专用按钮台;
专用按钮行;
专用按钮栏;
私人按钮日期;
公共静态void main(字符串[]args){
应用程序启动(args);
}
@凌驾
public void start(Stage primaryStage)引发异常{
initButton();
initTable();
主干=新阶段();
文件名=新标签(“第100号-测试数据(应用:测试)”;
mainsecene=新场景(mainVBox(),1200600);
tableView.prefWidthProperty().bind(mainBox.widthProperty());
tableView.prefHeightProperty().bind(mainBox.heightProperty());
主干。设置场景(主场景);
show();
}
私有网格窗格mainGrid(){
GridPane GridPane=新建GridPane();
gridPane.setVgap(10);
网格窗格。setHgap(10);
添加(文件名,0,0);
add(buttonBox(),0,1);
添加(tableView,0,2);
返回网格窗格;
}
专用VBox按钮盒(){
VBox buttonBox=新的VBox();
HBox firstRowBox=新的HBox();
HBox secRowBox=新的HBox();
firstRowBox.getChildren().addAll(打开、保存、neu、设置);
firstRowBox.setSpacing(5);
secRowBox.getChildren().addAll(表、行、列、日期);
secRowBox.setspace(5);
buttonBox.getChildren().addAll(firstRowBox,secRowBox);
按钮盒设置间隔(5);
buttonBox.prefWidthProperty().bind(mainBox.widthProperty());
返回按钮盒;
}
专用VBox mainVBox(){
mainBox=新的VBox();
mainBox.prefWidthProperty().bind(mainStage.widthProperty().multiply(0.8));
mainBox.setPadding(新插图(10,10,10,10));
mainBox.getChildren().add(mainGrid());
返回主机箱;
}
私有void initButton(){
打开=新按钮(“打开”);
打开。设置预宽度(100);
保存=新建按钮(“保存”);
save.setPrefWidth(100);
neu=新按钮(“新”);
neu.设置预宽(100);
设置=新按钮(“设置”);
设置。setPrefWidth(100);
表格=新按钮(“表格”);
表1.setPrefWidth(100);
行=新按钮(“行”);
行宽度(100);
列=新按钮(“列”);
列宽(100);
日期=新按钮(“日期”);
日期。设置预宽(100);
}
私有表视图initTable(){
tableView=新的tableView();
//创建列用户名(字符串的数据类型)。
TableColumn userNameCol//
=新表列(“用户名”);
//创建列电子邮件(数据类型为字符串)。
表列//
=新表格栏(“电子邮件”);
//为FullName创建2个子列。
TableColumn firstNameCol//
=新表列(“名字”);
表列lastNameCol//
=新表列(“姓氏”);
//活动柱
表列活性COL//
=新表列(“活动”);
tableView.getColumns().addAll(userNameCol、emailCol、firstNameCol、lastNameCol、activeCol);
对于(int i=0;tableView.getColumns().size()
最后,我希望在加载文件后,列的宽度仍然应该是TableView的宽度,除以列的数量,期望宽度<100px。在这种情况下,每个宽度应为100px,并显示一个滚动窗格(忽略此处的滚动条)
谢谢你的帮助 您的最小宽度约束从未设置
只要替换这个:
for (int i = 0; tableView.getColumns().size() < i; i++){
非常感谢。我几分钟前才发现。为什么所有的宽度绑定?很可能,它们在某个地方相互冲突。在调试布局问题时,通常最好不要对pref或任何其他大小属性进行外部设置/绑定,并了解自然发生的情况。调整这些自然尺寸应该是第二步。@kleopatra如vl4d1m1r4所说,唯一不起作用的是我的for循环。谢谢你的提示,下次我会做一个正确的mcve。它是正确的——正如你所看到的,它是可以回答的,一切都很好:)
for (int i = 0; i < tableView.getColumns().size(); i++) {
tableView.getColumns().forEach(column -> column.setMinWidth(100));