JavaFX: Force individual column width in TableView

2019-09-01 05:41发布

How can an individual column width be forced in a TableView?

Let's say I have a button to reset the width of all my columns to a pre-defined value. My TableView is configured to have UNCONSTRAINED_RESIZE_POLICY. Right now, if I resize two columns manually and then hit the reset button, nothing happens.

I'm using setPrefWidth() to set the preferred column width. I even checked the value before and after, it changes as desired. The visual representation, however, stays the same. Is there any possibility to force a real column resize to the desired width? Can I tell my table to re-layout, update or be re-drawn?

Does JavaFX provide a function such as pack() in SWT to force a component to resize to its preferred width/height?

1条回答
趁早两清
2楼-- · 2019-09-01 06:17

Try to bind the columns width to that of the table:

col1.prefWidthProperty().bind(col1.getTableView().widthProperty().multiply(1.5).divide(2));
col2.prefWidthProperty().bind(col2.getTableView().widthProperty().multiply(0.5).divide(2));
查看更多
登录 后发表回答