java - Ho to fill JTable in such a way that each cell contains a `String` consisted of two lines? -


is possible fill jtable in such way each cell contains string consisted of 2 lines?

string celltext = "line 1 \n line 2"; 

in jtable see celltext displayed single line.

you'll want use custom jtextarea renderer.

http://www.coderanch.com/t/340609/gui/java/jtable-custom-cell-renderer-jtextarea

this 1 i've used on years:

import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.util.*;  public class textarearenderer extends jtextarea implements tablecellrenderer {      private final defaulttablecellrenderer adaptee = new defaulttablecellrenderer();     /** map table map of rows map of column heights */     private final map cellsizes = new hashmap();       public textarearenderer() {         setlinewrap(true);         setwrapstyleword(true);     }      public component gettablecellrenderercomponent(             jtable table, object obj, boolean isselected,             boolean hasfocus, int row, int column) {          // set colours, etc. using standard platform         adaptee.gettablecellrenderercomponent(table, obj,                 isselected, hasfocus, row, column);         setforeground(adaptee.getforeground());         setbackground(adaptee.getbackground());         setborder(adaptee.getborder());         setfont(adaptee.getfont());         settext(adaptee.gettext());           // line important working jdk1.4         tablecolumnmodel columnmodel = table.getcolumnmodel();         setsize(columnmodel.getcolumn(column).getwidth(), 100000);         int height_wanted = (int) getpreferredsize().getheight();         addsize(table, row, column, height_wanted);         height_wanted = findtotalmaximumrowsize(table, row);         if (height_wanted != table.getrowheight(row)) {             table.setrowheight(row, height_wanted);         }         return this;     }      @suppresswarnings("unchecked")     private void addsize(jtable table, int row, int column, int height) {         map rows = (map) cellsizes.get(table);         if (rows == null) {             cellsizes.put(table, rows = new hashmap());         }         map rowheights = (map) rows.get(new integer(row));         if (rowheights == null) {             rows.put(new integer(row), rowheights = new hashmap());         }         rowheights.put(new integer(column), new integer(height));     }      /**      * through columns , renderer.  if      * textarearenderer, @ maximum height in      * hash table row.      */     private int findtotalmaximumrowsize(jtable table, int row) {         int maximum_height = 0;         enumeration columns = table.getcolumnmodel().getcolumns();         while (columns.hasmoreelements()) {             tablecolumn tc = (tablecolumn) columns.nextelement();             tablecellrenderer cellrenderer = tc.getcellrenderer();             if (cellrenderer instanceof textarearenderer) {                 textarearenderer tar = (textarearenderer) cellrenderer;                 maximum_height = math.max(maximum_height,                         tar.findmaximumrowsize(table, row));             }         }         return maximum_height;     }      private int findmaximumrowsize(jtable table, int row) {         map rows = (map) cellsizes.get(table);         if (rows == null) {             return 0;         }         map rowheights = (map) rows.get(new integer(row));         if (rowheights == null) {             return 0;         }         int maximum_height = 0;         (iterator = rowheights.entryset().iterator();                 it.hasnext();) {             map.entry entry = (map.entry) it.next();             int cellheight = ((integer) entry.getvalue()).intvalue();             maximum_height = math.max(maximum_height, cellheight);         }         return maximum_height;     } } 

which call this:

table.getcolumn("column").setcellrenderer(new textarearenderer()); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -