0

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?

user1438038
  • 5,821
  • 6
  • 60
  • 94

1 Answers1

0

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));
javaHunter
  • 1,097
  • 6
  • 9
  • I cannot set column width to a fixed value (e.g. ``300``) then. – user1438038 Dec 03 '14 at 10:48
  • You can set the prefWidth, but the container of the column, in this case the tableView, is the one that determines the width of the column. It tries to give each column its preferedWidth but it doesn't always succeed. Anyway try to avoid fixed size columns in tableView as this is bad practice – javaHunter Dec 03 '14 at 11:01
  • I don't want the column to have a fixed width, I just want to set it to a *specific* width. The user can still resize it afterwards. But unfortunately there is no ``setWidth()`` method that takes effect directly. Its just min, max and pref and JavaFX mixes them all up and finally does something unpredictable. :-) – user1438038 Dec 03 '14 at 16:43