Customized jtable for sorting, Windows Multi-choice, zebra crossings, tooltip

Source: Internet
Author: User
Package COM. browser; import Java. AWT. component; import Java. AWT. dimension; import Java. AWT. font; import Java. AWT. point; import Java. AWT. DND. dndconstants; import Java. AWT. event. mouseevent; import javax. swing. jtable; import javax. swing. listselectionmodel; import javax. swing. rowsorter; import javax. swing. event. listselectionevent; import javax. swing. event. mouseinputlistener; import javax. swing. plaf. basic. basictabl Eui; import javax. swing. table. defaulttablecellrenderer; import javax. swing. table. tablerowsorter;/*** custom jtable for sorting, Windows Multi-choice, zebra crossings, tooltip * @ author DL */public class jmytable extends jtable {public int m_nrow =-1; /** current column number */Public int [] m_nrows = NULL;/** multiple sequence numbers selected currently */Public jmytablemodel model; Public rowsorter sorter; public jmytable (jmytablemodel model) {super (model); setui (New mytableui (); th Is. model = model; sorter = new tablerowsorter (model); setrowsorter (sorter); setcolumnselectionallowed (false); setrowselectionallowed (true); setselectionmode (listselectionmodel. single_interval_selection); setselectionmode (listselectionmodel. multiple_interval_selection); setdefaultrenderer (object. class, new myrender ();} class myrender extends defaulttablecellrenderer {public component gettablecellrenderer Component (jtable table, object value, Boolean isselected, Boolean hasfocus, int row, int column) {super. gettablecellrenderercomponent (table, value, isselected, hasfocus, row, column); // zebra line if (row % 2 )! = 0) {setbackground (New Java. AWT. color (236,233,216);} else {setbackground (Java. AWT. color. white);} // select the reverse blue if (isselected) {setbackground (Java. AWT. color. blue);} // Font font = new font ("", Font. bold, 12); setfont (font); return this ;}/ ** implement Windows Multi-choice habits */class mytableui extends basictableui {protected mouseinputlistener createmouseinputlistener () {return New mymouseinputhandler ();} class mymouseinputha Ndler extends mouseinputhandler {private Boolean ignoredrag = false; Public void mousepressed (mouseevent e) {If (E. getclickcount () = 2) {table. clearselection ();} Point origin = E. getpoint (); int ROW = table. rowatpoint (origin); int column = table. columnatpoint (origin); If (row! =-1 & column! =-1) {If (table. iscellselected (row, column) {ignoredrag = true;} else {super. mousepressed (e) ;}} public void mouseclicked (mouseevent e) {super. mouseclicked (E);} public void mousedragged (mouseevent e) {If (! Ignoredrag) {super. mousedragged (E);} else {table. gettransferhandler (). exportasdrag (table, E, dndconstants. action_copy) ;}} public void mousereleased (mouseevent e) {If (ignoredrag) {super. mousepressed (E); ignoredrag = false;} super. mousereleased (e) ;}}/ *** <br> * method Description: select the row <br> * input parameter: <br> * return type: */Public void valuechanged (listselectionevent e) {// system. out. println ("valuechanged"); super. valuech Anged (E); m_nrows = getselectedrows (); If (m_nrows.length = 0) {// m_nrows = NULL is not selected; m_nrow =-1; return ;} if (m_nrows.length = 1) {// select a m_nrows = NULL; m_nrow = getselectedrow (); If (m_nrow! =-1) m_nrow = sorter. convertrowindextomodel (m_nrow);} else {// select multiple m_nrow =-1; for (INT I = 0; I <m_nrows.length; ++ I) {If (m_nrows [I]! =-1) m_nrows [I] = sorter. convertrowindextomodel (m_nrows [I]) ;}} repaint () ;}/ *** <br> method Description: tooltiptext * <br> input parameter: * <br> return type: */Public String gettooltiptext () {point Pt = getmouseposition (); If (Pt = NULL) return NULL; int ROW = rowatpoint (PT ); if (ROW =-1) return NULL; int Index = sorter. convertrowindextomodel (ROW); If (Index =-1) return NULL; dimension tablesize = getsize (); int width = tablesize. width; If (width> 500) width = 500; If (width <300) width = 300; string strtext = "<HTML> <Table width =" + new INTEGER (width-50) + "PT border =/" 0/"cellpadding =/" 0/"cellspacing =/" 0/"style =/" table-layout: fixed/">" + "<font face =/" dialog/"size = 3 color = Black>" + model. getvalueat (index ). tostring () + "</font> </table> 
Package COM. browser; import Java. util. vector; import javax. swing. table. using acttablemodel;/*** custom jtablemodel, use * @ author DL */public class jmytablemodel extends using acttablemodel {protected vector all = new vector () with custom jtable (); /** list of all data * // ** define the header */protected string [] columnnames;/** field type array */protected class [] types; /** can the field be edited to mark the array */protected Boolean [] canedit;/*** <br> method Description: obtain the number of columns in the table, inherit the required method of abstracttablemodel * <br> input parameter: * <br> return type: int column number */Public int getcolumncount () {return columnnames. length;}/*** <br> method Description: The column name of a column is obtained. It inherits the required method of abstracttablemodel * <br> input parameter: col column Index * <br> return type: String column name */Public String getcolumnname (INT col) {return columnnames [col];}/*** <br> method description: return field type * <br> input parameter: columnindex column Index * <br> return type: Class */public class getcolumnclass (INT columnindex) {return types [columnindex];} /*** <br> method Description: Set whether the field can be edited * <br> input parameter: rowindex row index; columnindex column Index * <br> return type: boolean */Public Boolean iscelleditable (INT rowindex, int columnindex) {return canedit [columnindex];}/*** <br> method description: get the cell content * <br> input parameter: rowindex row index; columnindex column Index * <br> return type: Boolean */public object getvalueat (INT rowindex, int columnindex) {// todo auto-generated method stubreturn (vector) All. elementat (rowindex )). elementat (columnindex);}/*** <br> method Description: Obtain the content of a row * <br> input parameter: rowindex row Index * <br> return type: boolean */public object getvalueat (INT rowindex) {// todo auto-generated method stubreturn all. elementat (rowindex);}/*** <br> method Description: Clear the table * <br> input parameter: * <br> return type: void */Public void empty () {All. removeallelements (); firetabledatachanged ();}/*** <br> method Description: Set the result set * <br> input parameter: temp result set * <br> return type: void */Public void setrs (vector VCT) {All. addall (VCT); firetabledatachanged () ;}/ *** <br> method Description: number of rows in the table * <br> input parameter: * <br> return type: int columns */Public int getrowcount () {// todo auto-generated method stubreturn all. size ();}}

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.