Error while trying to implement Jtextarea in Jtable

Asked

Viewed 99 times

0

I’m trying to use this JTextArea in some columns of mine JTable, but something’s going 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 wearing it to call the class

 jTable1.getColumnModel().getColumn(3).setCellRenderer(new TextAreaCellRenderer());

1 answer

2


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

How are you using DefaultTableModel, usually it starts the whole model with null values, and these null values fill the table rows. But in the row this.setText(value.toString()); from your Nderer, 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 mentioned line, otherwise it will continue to pop nullpointer.

  • Good evening @Articuno, that really was the problem. Thanks for your help! I have one more question. if you test this class in a column and write enough content, it increases the cell perfectly, but if you observe, it always writes in a straight line and you end up not seeing what wrote back. would like to know if it is possible, when reaching the edge of the column, he gives a line break, or else give a enter and he give the break.

  • @Rafaelchaves the Tablecellrenderer, as its name already says, only serves to "render" the contents of the table cell. If you’re writing, you’re editing the phone and not just rendering it. This is probably solved if you also implement a Tablecelleditor in this column, because the default is that a Jtextfield is displayed when you enter edit mode, you would need to implement the cited class so that a textarea is also displayed in edit mode.

  • Understood. You would have an example of how I can solve this problem? people will type in running and sometimes topical texts.. for this the need for line break. I would thank you too much from the account.

  • solved the problem here. I created a celleditor and it all worked out. thanks for the tip! The only problem I’m having is that my cell where I put the 'Textareacellrenderer' is not getting focus.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.