将鼠标悬停在另一个单元格上时如何删除 JTable 单元格 bgColor?

发布于 2024-09-08 16:11:06 字数 753 浏览 4 评论 0原文

我为 JTable 列创建了自己的 TableCellEditor,以便在编辑该列的单元格时执行一些特殊的操作。

在该 TableCellEditor 中,当该列中的单元格悬停时,我定义了一种颜色,如下所示:

public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {  
    if( isSelected )  // User clicked on this cell.
        setBackground( selectedRowBG );
    else if( rowIndexToHighlight == row  )  // user is hovering on this cell.
        setBackground( hoveredRowBG );
    else  // Set default cell color.
        setBackground( unHoveredRowBG );

    return this;
}

我的问题是,当我将鼠标悬停在该特殊列中的单元格上时,单元格背景颜色变为“hoveredRowBG”,然后如果我随将鼠标移动到另一列中的单元格或移动到表中的空白区域(没有行),特殊单元格 bgColor 仍然具有“hoveredRowBG”颜色。 我想在发生此类操作时删除悬停的颜色。

有什么想法吗?

I have created my own TableCellEditor for a JTable column to do some special stuff while editing the cells of this column.

In that TableCellEditor i define a color when a cell in that column is hovered, like this :

public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {  
    if( isSelected )  // User clicked on this cell.
        setBackground( selectedRowBG );
    else if( rowIndexToHighlight == row  )  // user is hovering on this cell.
        setBackground( hoveredRowBG );
    else  // Set default cell color.
        setBackground( unHoveredRowBG );

    return this;
}

My problem is when i hover with the mouse on a cell in that special column the cell background color becomes "hoveredRowBG", then if i move with the mouse to a cell in another column or move to empty space in the table(That has no rows), the special cell bgColor still has the "hoveredRowBG" color.
I want to remove that hovering color when such action happens.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-09-15 16:11:09

只需将实际突出显示的行存储在某处并在悬停时测试它是否已更改。如果是,则取消突出显示最后一个突出显示的内容并存储实际的内容。 lastHighlightedRow 在我的示例中是一个字段,您可能需要另一个位置来存储该值。

public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {  
    if( isSelected )  // User clicked on this cell.
        setBackground( selectedRowBG );
    else if( rowIndexToHighlight == row  ) {  // user is hovering on this cell.
        if (!(lastHighlightedRow == this)) {
           lastHighlightedRow.setBackground(unHoveredRowBG);
           lastHighlightedRos = this;
        }
        setBackground( hoveredRowBG );
    }

    return this;
}

Just store the actual highlighted row somewhere and test, while hovering, if it has changed. If yes, unhighlight the last highlighted and store the actual one. lastHighlightedRow is a field in my example, you may need another place to store the value.

public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {  
    if( isSelected )  // User clicked on this cell.
        setBackground( selectedRowBG );
    else if( rowIndexToHighlight == row  ) {  // user is hovering on this cell.
        if (!(lastHighlightedRow == this)) {
           lastHighlightedRow.setBackground(unHoveredRowBG);
           lastHighlightedRos = this;
        }
        setBackground( hoveredRowBG );
    }

    return this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文