I. facts to be aware:
1. When you hover your mouse over the jtable, the corresponding cell Renderer tablecellrenderer's rendering method (gettablecellrenerercomponent) will be called, but not timely enough (this can be done by printing a sentence in the rendering method from the row test ),
In addition, the rendering method of the lattice covered by the mouse is called, and the rendering method of other grids in the same line is not called. Therefore, it is impossible to achieve the goal simply by modifying the Renderer.
2. All jtable listeners are defined in tableui (basictableui is used by default). From the source code of basictableui, we can find that only mouseinputlistener is used for mouse listeners, does not support listening for mouse hover events. So, if you
Override tableui and add the mouse hover Action event listener to jtable. However, this implementation method is not discussed in this article.
Ii. Overall Thinking:
Can I rewrite tableui and use the most direct mouse listener for the purpose?
First, define tablecellrenderer, use the jtable object to obtain the row number of the current mouse row, and modify the background color according to the row number in the rendering method.
Then add a mouse behavior listener (mousemotionlistener) to the jtable. When a row of the jtable is monitored with a mouse hover, The preparerenderer method of jtable is triggered, prompting the grid in the corresponding row of the jtable to be rendered. Then call jtable repaint.
Iii. code snippets
// For convenience, the processing in the Renderer is written directly when jtable is constructed. Normally, you do not need to modify the jtable code and write the logic in the Custom tablecellrenderer.
Jtable table = new jtable (model ){
@ Override
Public component preparerenderer (tablecellrenderer Renderer, int row, int column ){
Component comp = super. preparerenderer (Renderer, row, column );
Point P = getmouseposition ();
If (P! = NULL ){
Int rowundermouse = rowatpoint (P );
If (rowundermouse = row ){
Comp. setbackground (color. Red );
} Else {
Comp. setbackground (defalooklookup. getcolor (this, UI, "table. alternaterowcolor "));
}
}
Return comp;
}
}
Class mytablemousemotionlistener extends mousemotionadapter {
Private int rowundermouse =-1;
@ Override
Public void mousemoved (mouseevent e ){
Jtable table = (jtable) E. getsource ();
Point P = table. getmouseposition ();
If (P! = NULL ){
Rowundermouse = table. rowatpoint (P );
If (rowundermouse> = 0 ){
For (INT I = 0; I <Table. getcolumncount (); I ++ ){
Table. preparerenderer (table. getcellrenderer (rowundermouse, I), rowundermouse, I );
If (rowundermouse! = 0 ){
Table. preparerenderer (table. getcellrenderer (rowUnderMouse-1, I), rowUnderMouse-1, I );
}
If (rowundermouse! = Table. getrowcount ()-1 ){
Table. preparerenderer (table. getcellrenderer (rowundermouse + 1, I), rowundermouse + 1, I );
}
}
Table. repaint (table. getvisiblerect ());
}
}
}
}
Finally, table. addmousemotionlistener (New mytablemousemotionlistener ());
Use the mouse listener to hover the mouse over a line in jtable to change the background color.