Skip to main content



      Home
Home » Eclipse Projects » NatTable » Column Width Listener(Is it possible to add a column width listener?)
Column Width Listener [message #1850957] Wed, 23 March 2022 07:27 Go to next message
Eclipse UserFriend
I operate a Nat table with hundreds of columns, which can be turned on and off by the user.
Many columns headers are alike - only grouped under different group headers.
I would like to add a column width listener, so if I adjust the column width, for let's say an address column, the width of all address columns are re-sized.

Best regards
Peter
Re: Column Width Listener [message #1850959 is a reply to message #1850957] Wed, 23 March 2022 08:38 Go to previous messageGo to next message
Eclipse UserFriend
Of course this can be done. On resize a ColumnResizeEvent is fired from the DataLayer.

You can either implement an ILayerEventListener and register it on a layer on top of the DataLayer. Note that ILayerEventListener are not called from the layer that fires them.

selectionLayer.registerEventHandler(new ILayerEventHandler<ColumnResizeEvent>() {

    @Override
    public void handleLayerEvent(ColumnResizeEvent event) {
        int[] columnIndexes = event.getColumnIndexes();
        if (columnIndexes.length == 1) {
            if (columnIndexes[0] == 1) {
                bodyDataLayer.setColumnWidthByPosition(2, bodyDataLayer.getConfiguredColumnWidthByPosition(1));
            }
        }
    }

    @Override
    public Class<ColumnResizeEvent> getLayerEventClass() {
        return ColumnResizeEvent.class;
    }
});


Or you implement an ILayerListener and register it on the DataLayer. This avoids probable position convertions, but the implementation of such a listener makes the event type check and cast necessary.

bodyDataLayer.addLayerListener(new ILayerListener() {
    @Override
    public void handleLayerEvent(ILayerEvent event) {
        if (event instanceof ColumnResizeEvent) {
            int[] columnIndexes = ((ColumnResizeEvent) event).getColumnIndexes();
            if (columnIndexes.length == 1) {
                if (columnIndexes[0] == 1) {
                    bodyDataLayer.setColumnWidthByPosition(2, bodyDataLayer.getConfiguredColumnWidthByPosition(1));
                }
            }
        }
    }
});


Note that you need to use getConfiguredColumnWidthByPosition() to get the configured value and not the scaled value for rendering.
Re: Column Width Listener [message #1851467 is a reply to message #1850959] Wed, 06 April 2022 06:48 Go to previous messageGo to next message
Eclipse UserFriend
Thanks Dirk
yes it works absolutely beautiful, however, I had to lift my development environment from NAT tables version 1.5.0 (2017) to 2.0.0 (2020 dec.)for this to work.

I chose the 2. solution and I can now adjust all similar columns by only adjusting one. :-)

I had to come up with a solution to prevent looping, where one column width change triggers the other - using a simple boolean.

Best regards
/Peter

Re: Column Width Listener [message #1851491 is a reply to message #1851467] Wed, 06 April 2022 10:10 Go to previous message
Eclipse UserFriend
Well it would also work with older versions, then the method is just setColumnWidth() and getColumnWidth() IIRC. But updating is always a good idea, since there where many bugfixes in the last 5 years, and with 2.0 it supports scaling. BTW, I would suggest to update to 2.0.2 to get the latest bugfixes.

And of course the snippet above is just a simple example. For a complete solution it needs to be extended to avoid loops etc.
Previous Topic:How to hide Images (Labels)
Next Topic:Combine Excel-like filters and drop-down lists with text search in the same table
Goto Forum:
  


Current Time: Mon May 12 21:45:14 EDT 2025

Powered by FUDForum. Page generated in 0.03618 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top