Hello all,
I have a problem with a jtable. I have written a program to simplify
the translation of the texts in my main application. So basically what
happen is that my editor reads the .properties file and lets the user
change the values of them.
The problem comes when the user is trying to view a value with html
text in it. What happened was that the html formatting contained
linebreaks and the default table cell renderer did the linebreaks BUT
the height of the cell did not update so all you could see was the
bottom of the first line and the top of the second.
I tried to fix this by using my own cell renderer that adjusted the
height if the preferred height of the renderer was greater than the
height of the row.
This almost worked, the row now becomes heigh enough to hold the whole
text BUT the cell is always rendered with an empty line at the top,
which means the last line is not visible. Any suggestions on how to
fix this?
I will try to make a minimal code example during the weekend.
I post the code for the cell renderer as it is short and possibly
(even probably) the cause of the problem
public class CellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c=
super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if(table.getRowHeight(row)<c.getPreferredSize().height){
table.setRowHeight(row,
c.getPreferredSize().height+table.getRowMargin());
}
return
super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
}
}
cheers
Daniel
alex@sibr.ru - 24 Nov 2006 04:14 GMT
> This almost worked, the row now becomes heigh enough to hold the whole
> text BUT the cell is always rendered with an empty line at the top,
> which means the last line is not visible. Any suggestions on how to
> fix this?
I'v faced the similar problem. When i put the html-text into table cell
which is using the DefaultTableCellRenderer (or my own JLabel-based
renderer), the displayed text begins from the the center of the cell,
with an empty space above it. Further, when the user manually resize
the column, the text aligns correctly. I've made an assumption, that
when JTable renders its cells for the first time, it doesn't take into
account the height of the cell. Row height is set in my program by
using the JTable.setRowHeight(int) method and is equal to all rows of
the table.
I spend three hours trying to solve a problem, and finally solve it by
put a JTable.setRowHeight(int) command into a
getTableCellRendererComponent() method of my renderer. So I think that
doing so is forcing the table to set row height at each render
operation. Looks ugly, but it works. So my renderer is looks in the
followng way:
private class MyRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(...) {
super.getTableCellRendererComponent(...);
table.setRowHeight(55);
return this;
}
}
alex@sibr.ru - 24 Nov 2006 05:25 GMT
sorry, in the code above there should be a condition:
if (table.getRowHeight()!=55) table.setRowHeight(55);