Forums Register Login

complex JTable cell mouseover issue

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi Gerald,
What about MouseMotionListerner? Won't the table be notified as the mouse is moving across it?
Hope this helps,
Michael Morris
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi Gerald,
See if something like this will work:

This would be an inner class of your JTable.
Hope this helps,
Michael Morris
+Pie Number of slices to send: Send
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
Do not threaten THIS beaver! Not even with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4997 times.
Similar Threads
JTable
JTable
JTable
JTable behaviour
Color in JTable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:26:51.