Error when trying to implement JTextArea in JTable

0

I'm trying to use this JTextArea in some columns of my JTable , but it's giving something wrong. I know it's wrong, but I'm using DefaultTableModel .

public class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
        public TextAreaCellRenderer() {
            setLineWrap(true);
            setWrapStyleWord(true);
            setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
            setMargin(new java.awt.Insets(5, 5, 5, 5));
        }
        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            // set color & border here              
            this.setText(value.toString());
            setText((value == null) ? "" : value.toString());
            setSize(table.getColumnModel().getColumn(column).getWidth(),
                    getPreferredSize().height);
            if (table.getRowHeight(row) < getPreferredSize().height) {
                table.setRowHeight(row, getPreferredSize().height );
            }
            return this;
        }
    }

And I'm using it to call the class

 jTable1.getColumnModel().getColumn(3).setCellRenderer(new TextAreaCellRenderer());
    
asked by anonymous 10.01.2018 / 12:48

1 answer

2

What is preventing the execution of this code is probably redundancy that you added to avoid nullpointer, but at the same time you kept a line that needed this check.

Since you are using DefaultTableModel , it usually starts the entire model with null values, and these null values fill the rows of the table. But in the this.setText(value.toString()); line of your renderer, you try to convert the value of the cell without checking if it is null, and just below you do:

setText((value == null) ? "" : value.toString());

This solves the problem, but you need to remove the previous line mentioned, otherwise you will continue to pop nullpointer.

    
10.01.2018 / 20:27