2

I have a MultilineCellRenderer which should wrap the multiple lines in JTable cell.

public class MultiLineTableCellRenderer 
        extends JTextArea implements TableCellRenderer {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public MultiLineTableCellRenderer() {
        setLineWrap(true);
        setWrapStyleWord(true);
        setOpaque(true);
    }

    public Component getTableCellRendererComponent(JTable table,
                                Object value, boolean isSelected,
                                boolean hasFocus, int row, int column) {
        setText(value.toString());//or something in value, like value.getNote()..
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
        setSize(table.getColumnModel().getColumn(column).getWidth(),
            getPreferredSize().height);
        if (table.getRowHeight(row) != getPreferredSize().height) {
            table.setRowHeight(row, getPreferredSize().height);
        }
        return this;
    }
}

I am creating and assigning this cell renderer as default cell renderer for myjtable(cnr_DATA)

MultiLineTableCellRenderer r = new MultiLineTableCellRenderer();
cnr_DATA.setDefaultRenderer(String.class, r);
cnr_DATA.setModel(new DefaultTableModel(data,columns){
    public Class getColumnClass(int col){
        return String.class;
    };
});

I am also updating the content of jtable dynamically from the database

   DefaultTableModel model = (DefaultTableModel)cnr_DATA.getModel();

removeAllCurrentRows(model);

Vector<DocumentRow> data = 
    RecycleSQL.readRecycledDocuments();//this line returning vector of objects
for(DocumentRow object:data)
    model.addRow(new string[]{object.getFilename(),
            object.getTitle(),object.getLastTouched()
                            ,object.getLastTouchedBy()});
model.setRowCount(data.size());
cnr_DATA.revalidate();

My problem is that mycellrenderer is not being called and is not wrapping the data. Could some one please advice me how to call it after updating the content of the JTable.

Update according to the suggestion of Madprogrammer and hovercraft

After modifying the code according to the suggestion. Now the cellrenderer is being called but it is resting the row height to default value. This is the logs from debug statement

[2013-08-02 01:20:53,335] [AWT-EventQueue-0] DEBUG MultiLineTableCellRenderer  - setting row height 3  128
[2013-08-02 01:20:53,335] [AWT-EventQueue-0] DEBUG MultiLineTableCellRenderer  - setting row height 3  16

which means, that after setting the height of the row correctly it is resting it back to the default value

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
Ashish
  • 14,295
  • 21
  • 82
  • 127
  • I suspect that the call to `setModel` may be resetting the default editors and it will be changing the column model. You might want to swap those statements – MadProgrammer Aug 02 '13 at 05:09
  • I am sorry but I did not get what do you want me to change (swap) – Ashish Aug 02 '13 at 05:17
  • Swap setting model and the renderer so that the model is set first and then the renderer – MadProgrammer Aug 02 '13 at 05:18
  • Finally fixed. I will provide the answer. Thanks a lot for hints – Ashish Aug 02 '13 at 05:27
  • 1
    the removeAll looks fishy: a) setRowCount is not needed after adding rows b) table.revalidate is not needed, provided the model complies to its notification contract (which DefaultTableModel does) If any of these seems to "fix" a problem, you are doing something wrong which has to be found and _really_ fixed. – kleopatra Aug 02 '13 at 10:16

1 Answers1

3

This looks suspect to me:

cnr_DATA.setDefaultRenderer(String.class, r); // ?? String.class

Are you sure that the model holds String data, and that the column type returned by getColumnClass(...) returns String? Consider instead setting the cell renderer for the column that needs it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • yes, it is storing string[] and the column type is string. I have modified my code according to that. – Ashish Aug 02 '13 at 05:07
  • From memory, `DefaultTableModel#getColumnClass` returns `Object`. The OP needs to override this method to return `String` - I'm giving you +1 cause you're close and I know you'll verify and update as required (besides, you're so close to 3 digits) ;) – MadProgrammer Aug 02 '13 at 05:22
  • Finally I have fixed it. I am going to write the answer now. Thanks a lot for your hints you are always great – Ashish Aug 02 '13 at 05:27
  • [see basic stuff](http://stackoverflow.com/a/12867849/714968), note carrefully with doLayout, is proper method for reasonable number of chars in JTextArea as renderers component, otherwise you need to use JScrollPane for JTextArea too, that I can't see in your renderer too – mKorbel Aug 02 '13 at 06:11