valueChanged() gets called multiple times in TreeSelectionListener

662 views Asked by At

I have a tree table and I'm adding a treeSelectionListener on it. On one selection of a row the function valueChanged gets called 4times. I managed to control it to 2times by checking getOldLeadSelectionPath() to null.

Is there a better way to limit it to 1?

treeTable.getTree().addTreeSelectionListener(new TreeSelectionListener() {
    @Override
    public void valueChanged(TreeSelectionEvent e) {
        if (e.getOldLeadSelectionPath() == null) {
            int row = treeTable.getTable().getSelectedRow();
            String colGid = treeTable.getTable().getModel().getValueAt(row, 1).toString();
            if (treeTable.getTree().getSelectionPath().getPathCount() == 3) {
                KPropertyTable columnProperty = createColumnProperty(colGid);
                propertyPanel.removeAll();
                propertyPanel.add(columnProperty, BorderLayout.CENTER);
                propertyPanel.updateUI();
            } else {
                propertyPanel.removeAll();
                propertyPanel.add(new JLabel("Select a column", SwingConstants.CENTER));
              }
        }
    }
});        
1

There are 1 answers

0
hungariandatabaseprogrammer On BEST ANSWER

I know this is silly, but I managed to overcome this, by making a global variable that gets raised every time valueChanged called, and then you can do something like this:

global_variable += 1;

if (global_variable % 4 == 0){
    //your code here
}