简体   繁体   English

谓词更改后如何更新ColourHighlighter(swingx)

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

I have a class called ErrorHighlighter which gets notified anytime a property called errorString is changed. 我有一个名为ErrorHighlighter的类,该类在任何时候更改了名为errorString的属性时都会得到通知。 Based on this propertychangeevent I update the HighLighterPredicate to highlight a particular row with a red background. 基于此属性更改事件,我更新了HighLighterPredicate以突出显示带有红色背景的特定行。

ErrorHighlighter receives the propertychangeevent, it also changes the HighlighterPredicate, but the table row does not get updated with red background. ErrorHighlighter接收到属性更改事件,它也更改了HighlighterPredicate,但是表行没有更新为红色背景。

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);
        }
    }
} 

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. 方法treeTable.getIndexForNodeData()实际上通过对基础树数据结构进行预遍历来返回该行的索引。 This includes a root node that is not being displayed on the jxtreetable. 这包括未在jxtreetable上显示的根节点。 Hence I needed to minus 1 from the index 因此我需要从指数中减去1

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. 如果有人想看看ColorHighlighter和属性更改侦听器的示例,我将离开而不是删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM