0

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Jay Patel
  • 378
  • 6
  • 18
  • Also consider [`TablePopupEditor`](http://stackoverflow.com/a/3591230/230513). – trashgod Sep 06 '13 at 10:39
  • 1
    whatever the problem, **don't** change the state of the table from inside the renderer: all parameters must be considered _strictly readonly_ – kleopatra Sep 06 '13 at 11:59

0 Answers0