question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TableView column resizer extension function

See original GitHub issue

One thing that frustrated me for awhile is having a way to fit TableView columns to the header and cell contents. Then I found a little gem in a Stack Overflow post.

I was able to come up with this class to expose that resizeColumnToFitContent()

public final class TableColumnResizer<T> extends TableViewSkin<T> {

    public TableColumnResizer(TableView<T> tableView) {
        super(tableView);
    }

    @Override
    public void resizeColumnToFitContent(TableColumn<T, ?> tc, int maxRows) {
      super.resizeColumnToFitContent(tc,maxRows);
    }
}

Then I can apply that TableViewSkin to the skinProperty(), and call a resizeOp function at any time to re-fit the columns.

var resizeOp: (() -> Unit) by singleAssign()

//layout built here

tableView.apply {
    val resizerSkin = TableColumnResizer<FTRecord>(this);
    skinProperty().set(resizerSkin)
    resizeOp = { columns.forEach { resizerSkin.resizeColumnToFitContent(it, 100) } }
}

It would be awesome if we could apply a resizeColumns() extension function to the TableView. The problem is I can’t think of an efficient way at the top of my head without extending TableView or doing some lazy operation that stores state elsewhere. It’s really too bad extension properties can’t be fields.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thomasnieldcommented, Mar 25, 2016

Brilliant! Works perfectly. Thanks Edvin, that was pretty clever.

0reactions
thomasnieldcommented, Mar 25, 2016

Okay awesome, ill play with it again when I get back to my tablet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaFX TableColumn resize to fit cell content - Stack Overflow
Save this question. Show activity on this post. I'm looking for a way to resize a TableColumn in a TableView so that all...
Read more >
TableView (JavaFX 8) - Oracle Help Center
Applies the currently installed resize policy against the given column, resizing it based on the delta value provided. ObjectProperty<Callback<TableView<S>, ...
Read more >
setAutoresizesAllColumnsToFit - Apple Developer
If flag is YES , the difference in width is distributed among the table view's table columns; if flag is NO , only...
Read more >
Resize Tables and Cells - Tableau Help
You can change the size of the rows, columns, and cells that compose a table. The best way to resize your table depends...
Read more >
TableView.AllowBestFit Property | WPF Controls
BestFitColumn method. To resize all columns, use the TableView.BestFitColumns method. End-users can resize columns to their optimal width (if allowed) by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found