• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

complex JTable cell mouseover issue

 
Gerald Tan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gerald Tan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic