Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiline JTable Cells with auto height- extra large first row
    primarykey
    data
    text
    <p>I am developing a Java Desktop Application (jdk1.6) with Swing. My problem is about Multi-line cells(text-wrapping) with auto-adjusting cell height property in JTable. </p> <p>I could already implement this structure in this way:</p> <ul> <li>Table has its own cellrenderer.</li> <li>Cells are JTextArea with wraptext=true</li> <li>I count the rows in the JTextArea after inserting the text to the cell and adjust the row height of related row accordingly.</li> <li>Cell width is automatically adjusted. (from preferred size)</li> </ul> <p>2 problems about this structure: </p> <p>1) During the execution of the program, it can count the rows and adjusts the row height properly. </p> <p>But at the first initialization (first setModel()) it calculates the row count of "the first cell" of the table,i.e (0,0), much more than it is. I have debugged the code and found that it counts the letters in the text and multiplies with row height 16. (as if the width of cell was 1 letter) . At the end I get a very very high first row. Other rows are ok.</p> <p>When I doesn't insert any text to (0,0), no problem occurs.</p> <p>2) Row count doesn't work when I disable the table auto-resize property and determine the cell widths manually.</p> <p>Here is my cell renderer: </p> <pre><code>public class MultiLineCellRenderer extends JTextArea implements TableCellRenderer { private JTable DependentTable; public MultiLineCellRenderer() { DependentTable=null; setLineWrap(true); setWrapStyleWord(true); setOpaque(true); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { (...) //some background color adjustments setText((value == null) ? "" : value.toString()); int numOfLines = getWrappedLines(this); // Counting the lines int height_normal = table.getRowHeight(row);// read the height of the row if(DependentTable == null) // for this case always null { if (height_normal &lt; numOfLines*16) { table.setRowHeight(row,numOfLines*16); } } else (...) return this; </code></pre> <p>}</p> <p>Here how I calculate number of rows:</p> <pre><code>public static int getWrappedLines(JTextArea component) { View view = component.getUI().getRootView(component).getView(0); int preferredHeight = (int)view.getPreferredSpan(View.Y_AXIS); int lineHeight = component.getFontMetrics( component.getFont() ).getHeight(); return preferredHeight / lineHeight; } </code></pre> <h2>------------------------------------</h2> <p>I removed the row-adjusting-code to the outside of the class. The table has a Model Listener now. The first row is not extreme large in this time. Calling this method works but the problem is about counting the wrapped lines. Every time I fill in the cell with a very long text, the line is wrapped properly but my counter returns 1. (code the wrappedline counter is above. same as the one in renderer. But It worked fine there) </p> <p>here is my model listener:</p> <pre><code>public class ModelListener implements TableModelListener { JTable mainTable; JTable depTable; public ModelListener(JTable m, JTable d) { mainTable = m; depTable = d; } public ModelListener(JTable m){ mainTable = m; depTable = null; } public void tableChanged(TableModelEvent tme) { int fRow = tme.getFirstRow(); int col = tme.getColumn(); JTextArea cellArea = (JTextArea)mainTable.getDefaultRenderer(Object.class); int numOfLines = getWrappedLines(cellArea); //countLines(); int height_normal = mainTable.getRowHeight(fRow); System.out.println("h normal:"+height_normal); System.out.println("numLines:"+numOfLines); System.out.println("value:"+mainTable.getModel().getValueAt(fRow, col)); System.out.println("width:"+cellArea.getPreferredSize().width); if(depTable == null) { if (height_normal &lt; numOfLines*16) { mainTable.setRowHeight(fRow,numOfLines*16); } } else { //(---) } mainTable.repaint(); </code></pre> <p>}</p> <p>The result of printlines:</p> <blockquote> <ul> <li>preferredHeight: 15 // from wrapped lines function</li> <li>lineHeight: 15 // from wrapped lines function</li> <li>h normal:25</li> <li>numLines:1</li> <li>value:looooooooooooooooooooooonng tesssssssssssssssssssssssst // appears to be 2 lines</li> <li>width:104</li> </ul> </blockquote> <p>Thanks in advance :)</p> <p>Isil</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload