How do I update a ColourHighlighter (swingx) when the predicate has changed

136 Views Asked by At

I have a class called ErrorHighlighter which gets notified anytime a property called errorString is changed. Based on this propertychangeevent I update the HighLighterPredicate to highlight a particular row with a red background.

ErrorHighlighter receives the propertychangeevent, it also changes the HighlighterPredicate, but the table row does not get updated with red background.

I also update the tooltip of the row. That does not get reflected either.

Please see the code below. Could someone please help?

public class ErrorRowHighlighter extends ColorHighlighter implements PropertyChangeListener {

    private Map<Integer, String> rowsInError;
    private SwingObjTreeTable<ShareholderHistoryTable> treeTable;

    public ErrorRowHighlighter(SwingObjTreeTable<ShareholderHistoryTable> treeTable) {
        super(CommonConstants.errorColor, null);
        this.treeTable = treeTable;
        rowsInError=new HashMap<Integer, String>();
        setHighlightPredicate(new HighlightPredicate() {
            @Override
            public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
                if(rowsInError.containsKey(adapter.row)){
                    return true;
                }
                return false;
            }
        });
        this.treeTable.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                int row=ErrorRowHighlighter.this.treeTable.rowAtPoint(e.getPoint());
                if(rowsInError.containsKey(row)){
                    ErrorRowHighlighter.this.treeTable.setToolTipText(rowsInError.get(row));
                }else{
                    ErrorRowHighlighter.this.treeTable.setToolTipText(null);
                }
            }
        });
    }

    public void highlightRowWithModelDataAsError(ShareholderHistoryTable modelData){
        int indexForNodeData = treeTable.getIndexForNodeData(modelData);
        if(indexForNodeData>-1){
            rowsInError.put(indexForNodeData, modelData.getErrorString());
            updateHighlighter();
        }
    }

    public void unhighlightRowWithModelDataAsError(ShareholderHistoryTable modelData){
        int indexForNodeData = treeTable.getIndexForNodeData(modelData);
        if(indexForNodeData>-1){
            rowsInError.remove(indexForNodeData);
            updateHighlighter();
        }
    }

    public void updateHighlighter(){
        treeTable.removeHighlighter(this);
        treeTable.addHighlighter(this);
    }


    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        ShareholderHistoryTable sourceObject= (ShareholderHistoryTable) evt.getSource();
        if(StringUtils.isNotEmpty(sourceObject.getErrorString())){
            highlightRowWithModelDataAsError(sourceObject);
        }else{
            unhighlightRowWithModelDataAsError(sourceObject);
        }
    }
} 
1

There are 1 best solutions below

0
On

This looks like a mistake on my part. The method treeTable.getIndexForNodeData() actually returns back the index of the row by doing a pre-order traversal of the underlying tree data structure. This includes a root node that is not being displayed on the jxtreetable. Hence I needed to minus 1 from the index

int indexForNodeData = treeTable.getIndexForNodeData(modelData)-1; 

This fixed the problem for me. I am leaving the post rather than deleting it if anyone wants to look at an example of a ColorHighlighter and a property change listener.