Return line in MySQL, jtable line break, auto row height, column width \ r \ n <br/>

Source: Internet
Author: User
Tags gety html tags stringbuffer

Two problems: The 1.mysql does not support line-wrapping, textarea in the return of the incoming and then came out to become a space.

The 2.JTable render default is inherited JLabel, and does not support line-wrapping display. In order to achieve a newline, one way is to write a new render inheritance JTextArea, but this is not good after the automatic adjustment column width, row height can not be adjusted according to the carriage return, but also the adjustment is not allowed, and super card ... This method of code can be self-search online. The second method is described below: The default render does not support line-wrapping display, we first realize that the cell can be wrapped. We add in front of the value of a cell, add it to the end, and place it in the middle of a newline.
< BR >


, and then the contents of that cell can be changed. However, it is found that the cell does not adjust the row height, resulting in only half of the two lines of text being seen. At this time, we can write a mouse event, so that jtable can be dragged through the mouse to adjust the height of the row, but also can write a method, automatically calculate the cell value
< BR >


The number of occurrences, and then the line height is automatically adjusted by table.setrowheight (Times + 1) *17. The following is the specific implementation code: The best way is to save the correct content in the database, such as Java query and put the results into the TableModel, the inside of the return character is replaced by
, and add HTML tags at both ends. But JTextArea content gettext into the database, in the database display as a space, the specific code "\ r", \ r in Java is also escaped, if we query from the database and then replace can only replace the space, this will be the normal space also as a carriage return. So I think the best way is to replace the return in the database with the
, the textarea entered into SQL was replaced by SQL with char (10), so we perform an update table_name SET COLUMN_NAME = Replace on the database each time we execute the query (column_name , CHAR (10), '
'), and then assign the query result to the TableModel loop, if (column== will appear in the column of carriage return), then add the HTML tag. public string Parsetowrap (String str) {StringBuffer SB = new StringBuffer (); Sb.append (""); Sb.append (str);//sb.append ( Str.replaceall ("\", "
"); Sb.append (" "); return sb.tostring ();} After doing the above, in the jtable binding Tablemodellistener, modify the contents of the cell upload will appear upload HTML tags, need to detect HTML tags at the time of uploading, some words deleted and then uploaded (my table only 13-15 columns Plus, So only 13-15 rows detected): public void tablechanged (TableModelEvent e) {int row = E.getfirstrow ();          int column = E.getcolumn ();         tablemodel model = (TableModel) e.getsource ();         string columnName = model.getcolumnname (column);         string data = (String) model.getvalueat (row, column);        if (column==13| | column==14| | column==15) {String str2 = ""; int total = 0;for (string tmp = data; TMP! = Null&&tmp.length () >=str2.length ();) { if (tmp.indexof (str2) = = 0) {   total + +;  } tmp = tmp.substring (1);        }if (total!=0) {data.substring (6, Data.length ()-7);}}        object num =model.getvalueat (row, 0);       try{         if (column==1 &&!isdatestringvalid (data)) {         joptionpane.showmessagedialog (NULL, "date format error");         }        else if (column==19) {         joptionpane.showmessagedialog (NULL, "Please modify the attachment in Configuration");         } Else{        stmt.executeupdate ("Update exception set" +columnname+ "= '" + Data+ "' Where NO =" +num);        }         }catch (Exception upe) {upe.printstacktrace (); Joptionpane.showmessagedialog (NULL, "No Permissions");}}} Detection
The number of implementations automatically adjusts the row height (my table only 13-15 columns will appear carriage return, so do not start from 0 cycle): public void autoheight (JTable table) {TableModel Model=table.getmodel (); for (int i=0;iint height=17;for (int j=13;j<16;j++) {String str1 = (string) model.getvalueat (i, j); String str2 = "int total = 1;for (String tmp = STR1; TMP! = Null&&tmp.length () >=str2.length ();) { if (tmp.indexof (str2) = = 0) {   total + +;  } tmp = tmp.substring (1);} Height=math.max (height, 17*total);} Table.setrowheight (i, height);}} Detects the number of single-line characters, enabling AutoFit column widths: public void Fittablecolumns (JTable myTable) {   jtableheader Header = Mytable.gettableheader ();    int rowCount = Mytable.getrowcount ();    enumeration columns = Mytable.getcolumnmodel (). GetColumns ();    while (Columns.hasmoreelements ())     {    tablecolumn column = (tablecolumn) columns.nextelement ();        int col = Header.getcolumnmodel (). Getcolumnindex (Column.getidentifieR ());        int width = (int) mytable.gettableheader (). Getdefaultrenderer (). Gettablecellrenderercomponent (MyTable, Column.getidentifier (), False, False,-1, col). getPreferredSize (). GetWidth () ;                       for (int row = 0; row        {         int preferedwidth = (int) mytable.getcellrenderer (row, col). Gettablecellrenderercomponent ( Mytable,            mytable.getvalueat (Row, col), False, False, row, col). getPreferredSize (). GetWidth ();             width = Math.max (width, preferedwidth);        }         header.setresizingcolumn (column); This trip is important &NBSP;&NBSP;&NBSP;&NBSP;&NBSp;   column.setwidth (Width+mytable.getintercellspacing (). width);     }} To bind a mouse event to a table, you can manually adjust the row height: table.addmouselistener (listener); Table.addmousemotionlistener (listener); class Mouseadapterlistener extends Java.awt.event.mousemotionadapter    implements MouseListener { int OldY = 0;int Newy = 0;int row = 0;int OldHeight = 0;boolean drag = false;int Increase = 0; public Mouseadapterlis Tener () { } public void mousemoved (MouseEvent e) {    int Onrow = Table.rowatpoint ( E.getpoint ());      int height = 0;    for (int i = 0; I <= onRow; i++) {&N Bsp;       height = height + table.getrowheight (i);     }      if (Height-e.gety () < 3) {        drag = True;        table.setcursor (New Cursor (Cursor.n_resize_cursoR));     } else {        drag = false;         table.setcursor (New Cursor (cursor.default_cursor));     } } public void mousedragged (MouseEvent e) {    if (drag) {         int value = OldHeight + e.gety ()-Oldy;        if ( Value <             table.setrowheight (Row, 17) ;         else                                   table.setrowheight (row, OldHeight + e.gety ()-OldY);                 table.setrowselectioninterval (row, row);                     }} public void mousepressed (MouseEvent e) {    oldy = E.gety ();     row = Table.rowatpoint (E.getpoint ());     oldheight = Table.getrowheight (row);     table.setrowselectioninterval (row, row);}  public void mousereleased (MouseEvent e) {    newy = E.gety ();     Table.setcursor (New Cursor (Cursor.default_cursor));}  public void mouseclicked (MouseEvent e) {} public void mouseentered (MouseEvent e) {} public void mouseexited (MouseEvent e) {}}

Return line in MySQL, jtable line, automatically adjust row height, column width  \r \n <br/>

Related Article

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.