如何在谓词发生变化时更新Color Highlighter(swing)

时间:2013-06-01 09:17:13

标签: java swing swingx jxtreetable swing-highlighter

我有一个名为ErrorHighlighter的类,只要更改了名为errorString的属性,就会收到通知。基于此propertychangeevent,我更新HighLighterPredicate以突出显示具有红色背景的特定行。

ErrorHighlighter收到propertychangeevent,它也会更改HighlighterPredicate,但表行不会更新为红色背景。

我还更新了行的工具提示。这也没有反映出来。

请参阅下面的代码。有人可以帮忙吗?

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 个答案:

答案 0 :(得分:1)

这看起来像是我的错误。方法treeTable.getIndexForNodeData()实际上通过对底层树数据结构进行预顺序遍历来返回行的索引。这包括未在jxtreetable上显示的根节点。因此我需要从索引

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

这解决了我的问题。如果有人想查看ColorHighlighter和属性更改侦听器的示例,我将离开帖子而不是删除它。