Igor
Igor

Reputation: 1592

Multiline JTable Cells are not multilined during editing

I'm developing an app which has an JTable that needs to have multiline cells. Therefore I extended JTextArea and everything is shown noce, but when I try to edit a cell. the text is shown in a single line, and becomes multilined after edit. I want the text to stay multilined during editting. Is there a way to do that?

Upvotes: 2

Views: 2004

Answers (1)

Gilberto Torrezan
Gilberto Torrezan

Reputation: 5267

Create your TableCellEditor using a JTextArea (instead of the default behaviour which uses JTextField) and set it to your JTable.

You can use a JEditorPane as well to support text styling, if you wish.

---- Edit2 ----

New TableCellEditor:

 class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {

 JComponent component = new JTextArea();

 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
 int rowIndex, int vColIndex) {

 ((JTextArea) component).setText((String) value);

 return component;
 }

 public Object getCellEditorValue() {
 return ((JTextArea) component).getText();
 }
 }

Upvotes: 8

Related Questions