user1631306
user1631306

Reputation: 4470

addListSelectionListener invoke every time there is change in selection

I have Jtable with some rows. If I make selection to some rows, the addListSelectionListener gets invoked each time(for each row selection). Is there any way I can avoid these multiple invoking, as it slows down the process if I select 10000s of rows

Code which shows the selection of the rows

for (int i = 0; i < sArray.length; i++)
        {
            // int viewindex = mTableForActionListener.getRowSorter().convertRowIndexToView(i);
            mTableForActionListener.addRowSelectionInterval(sArray[i].intValue(), sArray[i].intValue());
        }

mDocListTable is instance of mTableForActionListener. Code where it gets invoked

mDocListTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
    {
        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            handle_TableSelection(e);
        }
    });

Upvotes: 0

Views: 363

Answers (1)

lkq
lkq

Reputation: 2366

Solution 1:

Remove the listener before your for loop and add it back after for loop.

ListSelectionListener listener = new ListSelectionListener()
{
    @Override
    public void valueChanged(ListSelectionEvent e)
    {
        handle_TableSelection(e);
    }
};

mDocListTable.getSelectionModel().removeListSelectionListener(listener);    
for (int i = 0; i < sArray.length; i++)
{
    mTableForActionListener.addRowSelectionInterval(sArray[i].intValue(), sArray[i].intValue());
}
mDocListTable.getSelectionModel().addListSelectionListener(listener);
// add some code here to deal with the new selections in the table.

Solution 2:

In the ListSelectionEvent, check if there's still adjusting taking place.

mDocListTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
{
    @Override
    public void valueChanged(ListSelectionEvent e)
    {
        if(e.getValueIsAdjusting()) return;
        handle_TableSelection(e);
    }
});

Upvotes: 1

Related Questions