user988788
user988788

Reputation: 21

JTable.SetCursor not changing cursor

I've created a table, and setcursor on a particular column. The cursor is displayed properly for the first time when table is displayed, but if the table model changes and table is repainted, the cursor is not shown as per the code, instead, default cursor is shown on all columns.

Tried debugging the code, the call to jtable.setcursor gets executed properly, but cursor not displayed on screen.

Have set cursor through MouseMotionListener inside MouseMoved() method.

Any help appreciated.


My Recent Observation:

I have detected that the issue arises when any other dialog comes over the scrollpane that displays the table. If a JFileChooser or a JDialog is launched over a panel containing JScrollPane for JTable, after closing the JFileChooser or JDialog, the cursor on table is not displayed as per code, this is happening even in case when table model does not change at all.

Following is my code to set cursor:

final Cursor handCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
final Cursor defaultCursor = Cursor.getDefaultCursor();
table.addMouseMotionListener(new MouseAdapter() 
{
    public void mouseMoved(MouseEvent e) 
    {
        int cModel = table.columnAtPoint(e.getPoint());
        int cView = table.convertColumnIndexToView(cModel);
        if (cView == 1)
        {
            table.setCursor(handCursor);                             
        }
        else
        {
            table.setCursor(defaultCursor);
        }
    }
});

Upvotes: 1

Views: 3077

Answers (3)

user988788
user988788

Reputation: 21

I had not set the owner of JFileChooser/Jdialog to the underlying panel. Hence, when these components came over, a new hierarchy was created and all painting operation started on this new hierarchy (something like this, but i'm not too sure).

When i set the owner of secondary panels as the main panel, on disposing these secondary panels, the modified cursor again started appearing on the main panel (might be due to the fact that a single paint hireracrchy was present now).

Upvotes: 1

mKorbel
mKorbel

Reputation: 109813

Have set cursor through MouseMotionListener inside MouseMoved() method.

no, I think that there any reason for that

you can solve that

1) define the Cursor for JScrollPane#setCursor() if you need to recreate JTable from its Model

2) stop to recreate JTable (from your TableModel) inside your code,

3) example for JTable and Cursor here

Upvotes: 1

StanislavL
StanislavL

Reputation: 57381

May be when model changes columns model is changed as well and the old column instance isn't valid anymore.

Upvotes: 1

Related Questions