Use the mouse listener to hover the mouse over a line in jtable to change the background color.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.