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());
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.
– Rafael Chaves
@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.
– user28595
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.
– Rafael Chaves
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.
– Rafael Chaves