Now this, is the best code snippet I found in Java that can auto adjust the JTable row height regardless what kind of renderer component is used.
I had been using some short code snippet that sets a JTextPane or JEditorPane content, then getting the height. However, when my content was full of HTML tags, the height returned really messed my JTable.
This code I found in Stack Overflow by user camickr really is the best solution for auto adjusting all rows heights of a JTable based on content.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
try { for (int row=0; row<table.getRowCount(); row++) { int rowHeight = table.getRowHeight(); for (int column=0; column<table.getColumnCount(); column++) { Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column); rowHeight = Math.max(rowHeight, comp.getPreferredSize().height); } table.setRowHeight(row, rowHeight); } } catch(ClassCastException e) { } |