SpreadsheetView - Constrain or resize column widths to parent container

287 views
Skip to first unread message

Mark du Mez

unread,
Nov 9, 2017, 8:46:59 PM11/9/17
to ControlsFX
Hi,

I realise that this in many ways runs counter to the use-case for SpreadsheetView, but I thought I'd ask anyway:

I've got a SpreadsheetView with several columns in it, which when the application is default size are wider than the parent container, and trigger a scroll.  This is fine, and I'm happy with this, but when I maximise the application, I'd like the rows to automatically resize the width of the parent, rather that staying the same size.  I'm initialising the component as follows:

private void buildAgencySpreadSheetView() {
   
GridBase grid = new GridBase(dataList.size(), 7);
   
spreadsheetView = new SpreadsheetView(grid);
   
spreadsheetView.setShowRowHeader(true);
   
spreadsheetView.setShowColumnHeader(true);
   
spreadsheetView.setEditable(true);

   
spreadsheetView.getColumns().get(0).setPrefWidth(120);
   
spreadsheetView.getColumns().get(1).setPrefWidth(120);
   
spreadsheetView.getColumns().get(2).setPrefWidth(200);
   
spreadsheetView.getColumns().get(3).setPrefWidth(350);
   
spreadsheetView.getColumns().get(4).setPrefWidth(350);
   
spreadsheetView.setFixingColumnsAllowed(true);
   
spreadsheetView.resizeRowsToMaximum();
   
borderPane.setCenter(spreadsheetView);
}

Looking at some other examples, it seemed that I would have better luck with not setting prefWidth, but this doesn't resize the columns to fit the data when I add it later (lots of ... going on, and the columns are all about 100 wide).

Is there a means by which the columns can be made to auto resize, and if not, what is the best approach for implementing it?

Thanks

samir.ha...@gmail.com

unread,
Nov 13, 2017, 4:36:02 AM11/13/17
to ControlsFX
Hi Mark,

I don't quite understand your issue. Is it about the height of the rows or about the width of the columns?

If you are talking about the columns, I'm afraid there is no auto resize available. The methods available for resizing columns depends on the data and not on the space available on the parent container.

However, you could always listen on the width of the parent container, and compute the width of your columns accordingly?

Regards,

Mark du Mez

unread,
Nov 13, 2017, 4:01:24 PM11/13/17
to ControlsFX
Hi,

Yes, I am referring to the column widths.

OK, I figured this might be the case.  Time to build me a listener, it seems.

Cheers,
Mark

Mark du Mez

unread,
Nov 13, 2017, 4:39:14 PM11/13/17
to ControlsFX
For any folk who might be coming to this off google, it's as simple as doing a:

parent.widthProperty().addListener((obs, oldval, newval) -> {
   
//Do your colum width calcs in here, and set prefWidth() on the appropriate columns
});
Reply all
Reply to author
Forward
0 new messages