简体   繁体   English

为特定的 JTable 单元格设置鼠标 cursor

[英]Setting the mouse cursor for a particular JTable cell

I have a JTable with a set of uneditable cells and I want all the cells in a particular column to have a different mouse cursor displayed whilst the mouse is hovering over them.我有一个 JTable 和一组不可编辑的单元格,我希望特定列中的所有单元格在鼠标悬停在它们上方时显示不同的鼠标 cursor。 I am already using a custom renderer and setting the cursor on the renderer component doesn't seem to work (as it does for tooltips).我已经在使用自定义渲染器并且在渲染器组件上设置 cursor 似乎不起作用(就像它对工具提示一样)。

It does seem to work for editors.它似乎确实适用于编辑器。

Is this not possible in JTable when your cell is not being edited or am I missing something?当您的单元格未被编辑或我遗漏了什么时,这在 JTable 中是不可能的吗?

将MouseMotionListener添加到JTable,然后在mouseMoved()上使用JTable的columnAtPoint()确定它是哪一列,如果它是您所追踪的特定列,则在JTable上设置setCursor()。

Here is one way of changing the cursor at a particular column in JTable: 以下是在JTable中更改特定列的光标的一种方法:

if(tblExamHistoryAll.columnAtPoint(evt.getPoint())==5)
{
    setCursor(Cursor.HAND_CURSOR); 
}
else
{
    setCursor(0);
}

I wanted to only change the cursor when the mouse was over the text in the cell.我只想在鼠标悬停在单元格中的文本上时更改 cursor。 This was my solution:这是我的解决方案:

private JTable table = ...;

@Override
public void mouseMoved(MouseEvent e) {
    Point point = e.getPoint();

    int column = table.columnAtPoint(point);
    int row = table.rowAtPoint(point);

    Component component = table.getCellRenderer(row, column).getTableCellRendererComponent(table,
            getValueAt(row, column), false, false, row, column);
    Dimension size = component.getPreferredSize();

    Rectangle rectangle = table.getCellRect(row, column, false);
    rectangle.setSize(size);

    if (rectangle.contains(point)) {
        table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        return;
    }

    table.setCursor(Cursor.getDefaultCursor());
}

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

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