aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes complex JTable cell mouseover issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "complex JTable cell mouseover issue" Watch "complex JTable cell mouseover issue" New topic
Author

complex JTable cell mouseover issue

Gerald Tan
Greenhorn

Joined: Jul 17, 2002
Posts: 7
To all the Java experts out there:
What/how should I code
so that a table cell background
color will change when my mouse
is over it? This should only
happen for the first column of the Jtable.
Thanks.
Warmly,
Gerald Tan
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Gerald,
Well, probably the best way to proceed would be to create your own table model, extending AbstractTableModlel and create custom cell renderers which would implement TableCellRenderer. If you are only interested in highlighting the first column, then all members of it should be of a particular class, so the JTable will pick that renderer. Actually you could just "lie" and return some appropriate class from the getColumnClass method in your table model. You should also install a MouseListener or MouseMotionListener in your JTable to trigger the rendering.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Gerald Tan
Greenhorn

Joined: Jul 17, 2002
Posts: 7
Michael,
Thanks, for your assistance. I tried what you suggested. The problem seems to be that adding the mouse listener to the table isn't enough. This way, when the mouse moves from cell (0,0) to below, cell(1,0), the mouseListener is not alerted because it is still in the table (the mouseListener is attached to the table). it seems that I need some other way to do this (attaching the listener to the cells themselves).
Anyone has thoughts?
Thanks
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Gerald,
What about MouseMotionListerner? Won't the table be notified as the mouse is moving across it?
Hope this helps,
Michael Morris
Gerald Tan
Greenhorn

Joined: Jul 17, 2002
Posts: 7
Michael,
Thanks for taking me a step to the right direction. I think I have one more final hurdle. So we attach the mousemotionlistener to the table and it is alerted each time the mouse moves. The thing is -- how do I identify which CELL is the mouse over? (I'd like to change the color of the cell with the mouse over it).
You've been really helpful. I really appreciate it.
Warmly,
Gerald
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Gerald,
See if something like this will work:

This would be an inner class of your JTable.
Hope this helps,
Michael Morris
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
you get a grip on the cell itself through the cellrenderer which normally is a JLabel. you can attach a mouselistener to it and listen for mouse entered and mouse exited events.
cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: complex JTable cell mouseover issue