I am facing the problem having JTable. In this jtable I want to wrap the text in same cell if the text is too long to be displayed in one line.
Row height should be automatically adjusted to fit the text.
I am doing something like :
public class MyCellRenderer extends JTextArea implements TableCellRenderer {
public MyCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
try
{
setText(value.toString());
}
catch(NullPointerException npe)
{
java.util.logging.Logger.getLogger(MyCellRenderer.class.getName()).log(java.util.logging.Level.SEVERE, null, npe);
}
setSize(table.getColumnModel().getColumn(column).getWidth(),
getPreferredSize().height);
if (table.getRowHeight(row) != getPreferredSize().height) {
table.setRowHeight(row, getPreferredSize().height);
}
return this;
}
}
and in the class having jtable I am adding:
jTable1.getColumnModel().getColumn(2).setCellRenderer(new MyCellRenderer());
but by doing this, If I am inserting data in 1st row, 3rd column this data is printed in all the cells automatically. I want to add separate text in each cell.