JavaFX 2 Automatic Column Width

2020-01-26 04:54发布

I have a JavaFX 2 table that is displaying contact details for people, lets imagine there are three columns: first name, last name and email address. When my application starts it populates the table with several rows of data about the people already in the system.

The problem is that the column widths are all the same. Most of the time the first and last name is displayed in full but the email address is getting clipped. The user can double click the divider in the header to resize the column but that will become tedious quickly.

Once the table has been pre-populated I would like to programatically resize all the columns to display the data they contain but I can't figure out how to achieve this. I can see that I can call col.setPrefWidth(x) but that doesn't really help as I would have to guess the width.

标签: javafx-2
8条回答
放我归山
2楼-- · 2020-01-26 05:09

This works for me in JavaFX 8

table.setColumnResizePolicy( TableView.CONSTRAINED_RESIZE_POLICY );
col1.setMaxWidth( 1f * Integer.MAX_VALUE * 50 ); // 50% width
col2.setMaxWidth( 1f * Integer.MAX_VALUE * 30 ); // 30% width
col3.setMaxWidth( 1f * Integer.MAX_VALUE * 20 ); // 20% width

In the other examples you have the problem, the vertical scrollbar width is ignored.

查看更多
爷的心禁止访问
3楼-- · 2020-01-26 05:10

If you had 4 columns and only last column needed to expand to fill the rest of the table width and the other columns remained the size I set in Scene Builder.

double width = col1.widthProperty().get();
width += col2.widthProperty().get();
width += col3.widthProperty().get();

col4.prefWidthProperty().bind(table.widthProperty().subtract(width));

Or if you had 2 columns that needed to expand then.

double width = col1.widthProperty().get();
width += col3.widthProperty().get();

col2.prefWidthProperty().bind(table.widthProperty().subtract(width).divide(2));
col4.prefWidthProperty().bind(table.widthProperty().subtract(width).divide(2));
查看更多
相关推荐>>
4楼-- · 2020-01-26 05:18

You can also do it by editing the fxml file if you have one.

<TableColumn fx:id="blackout_number_column" prefWidth="30.0" text="%number">

The parameter prefWidth allows you to modify the default with of a column.

查看更多
beautiful°
5楼-- · 2020-01-26 05:20

To generelize the width and use any number and make sure that the width is 100% of the table's width, you can use :

double[] widths = {20, 30, 50, 20, 30, 70, 50};//define the width of the columns

//calculate the sum of the width
double sum = 0;
for (double i : widths) {
    sum += i;
}

//set the width to the columns
for (int i = 0; i < widths.length; i++) {
    table.getColumns().get(i).prefWidthProperty().bind(
            table.widthProperty().multiply(sizes[i] / sum));
    //---------The exact width-------------^-------------^
}
查看更多
迷人小祖宗
6楼-- · 2020-01-26 05:21

If your total number of columns are pre-known. You can distribute the column widths among the tableview's width:

nameCol.prefWidthProperty().bind(personTable.widthProperty().divide(4)); // w * 1/4
surnameCol.prefWidthProperty().bind(personTable.widthProperty().divide(2)); // w * 1/2
emailCol.prefWidthProperty().bind(personTable.widthProperty().divide(4)); // w * 1/4

In this code, the width proportions of columns are kept in sync when the tableview is resized, so you don't need to do it manually. Also the surnameCol takes the half space of the tableview's width.

查看更多
Luminary・发光体
7楼-- · 2020-01-26 05:24

As I use SceneBuider, I just define the MinWidth, and MaxWidth to some columns and to the main column I just define the PrefWidth to "USE_COMPUTED_SIZE"

查看更多
登录 后发表回答