Search code examples
nattable

Using NatTable's ShowRowInViewportCommand


I was trying with NatTableExamples-1.4.0 and was trying to use ShowRowInViewportCommand with _900_test\viewportSelection\ViewportSelectionHideShowDataLayerExample.

I stored the nattable object obtained from createExampleControl method and overloaded onStart() method as follows:

@Override
public void onStart() {
        nattable.doCommand(new ShowRowInViewportCommand(nattable.getLayer(), 35));    //didn't work
        nattable.doCommand(new SelectRowsCommand(nattable.getLayer(), 0, 35, false, false)); //worked, can see the row selected after scrolling down
    }

Here SelectRowsCommand worked. I saw that row 36 gets selected due to Index-position transformations, .

But I did not see the effect of ShowRowInViewportCommand in UI. I was expecting that the row 36 should be seen automatically .

ShowRowInViewportCommandHandler is in place. During debugging, I verified that the control reached to ShowRowInViewportCommandHandler.doCommand method.

What should I do to see automatic scrolling?


Solution

  • You have a timing problem in your approach. You want to make something visible and scroll to that position that has not even been rendered yet. How should the scrolling be executed on something that is not rendered? The Shell is opened AFTER onStart().

    To execute auto-scrolling on startup you need to implement a listener that executes after rendering is done, e.g. by using a PaintListener.

    this.nattable.addPaintListener(new PaintListener() {
    
        @Override
        public void paintControl(PaintEvent e) {
            nattable.doCommand(new SelectRowsCommand(nattable, 0, 35, false, false));
            nattable.removePaintListener(this);
        }
    });
    

    BTW, you don't even need to execute the ShowRowInViewportCommand as the SelectRowsCommand automatically moves the selected row into the viewport.