Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adapting row height in JFace TableViewer as I scroll my table

I have a JFace TableViewer with an ILazyContentProvider and a StyledCellLabelProvider for each column, which I mostly grabbed from https://wiki.eclipse.org/JFaceSnippets#Snippet006TableMultiLineCells to enable multiline rows. When I open the table, all rows have the height of the row which takes up the most space, as intended. As I scroll down the table, the row heights will change as intended if a row takes up more space. However, this does not currently work in the other direction, i.e., if I scroll so that the current rows showing take up less space, all rows still have the height of the largest row in the whole table.

Is there a way to solve this? Somehow there seems to be a memory of the content that the lazy content provider should be forgetting?

This is my measure method in the StyledCellLabelProvider:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@Override
protected void measure(Event event, Object element) {
    event.width = viewer.getTable().getColumn(event.index).getWidth();
    if (event.width == 0) {
        return;
    }
    TableEntryData rowData = (TableEntryData) element;
    TableCellData cellData = getCellData(rowData, event.index);
    int height = event.gc.textExtent(SOME_STRING).y; // Height of a written string on one line.
    int lines = cellData.getPoints().size();
    event.height = height * lines;

    event.gc.dispose();
}

and this is most of my ILazyContentProvider:

@Override
public void updateElement(int index) {
    viewer.replace(entries.get(index), index);

}

@SuppressWarnings("unchecked") // TODO:
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    this.entries = (List<TableEntryData>) newInput;
}

>Solution :

The measure item code in the Table class (used by TableViewer) will never reduce the size of rows once it has grown bigger, so there is no way to change this.

The measure code is Table.sendMeasureItem but it can’t be overridden. The code here is platform specific, but I checked both the Windows and macOS version (not sure about the Linux/GTK version).

I do have a hack to work around this, but it is platform dependent and I only have it for macOS.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading