Ashish
Ashish

Reputation: 14707

Unable to Call the JTable Cellrenderer in Jtable

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

Upvotes: 2

Views: 321

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

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.

Upvotes: 3

Related Questions