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?
Asked
Active
Viewed 1,981 times
2
-
2Have you read the tutorial? If you did you'd see that there's a difference between the editor and the renderer. If you did read the tutorial, then you should know this and tell us about both your editor and renderer. Bad question as it is very short on necessary details. – Hovercraft Full Of Eels Sep 11 '12 at 22:18
-
1See also [`TablePopupEditor`](http://stackoverflow.com/a/3591230/230513). – trashgod Sep 12 '12 at 00:01
1 Answers
8
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();
}
}

Igor
- 1,532
- 4
- 23
- 44

Gilberto Torrezan
- 5,113
- 4
- 31
- 50
-
2@Igor re-read the answer. It says you should use a JTextArea for your TableCellEditor. As far as I understand your question, you are using a JTextArea for your TableCellRenderer. See more information [here](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) on renderers and editors. – Guillaume Polet Sep 11 '12 at 22:13
-
All I can understand from your question is that you're using a custom TableCellRenderer but not a custom TableCellEditor. If it's not the case, then you should try to investigate why your line breaks are not transferred to your editor when you edit the cell. Or you could use another component to edit it. – Gilberto Torrezan Sep 11 '12 at 22:13
-
@Igor His answer is correct. Note he said `TableCellEditor` instead of `TableCellRenderer`. The editor is what's shown when the user edits the cell, and it's different from the renderer. If you did this and you're still having problems, try setting the number of rows on the editor with `JTextArea.setRows` (though I don't think this will be necessary). – Brian Sep 11 '12 at 22:14
-
Sorry, my bad. I thought it's the same thing. That could solve my problem. Can you add a short example in the answer before I chose it as an answer. Thanx. – Igor Sep 11 '12 at 22:18
-
Thanx, while waiting I created a celleditor of my own. It all works, ** except that it doesn't add new lines until I click away after editing the cell.** Anyway I'm gonna choose your answer, as it describes the procedure well enough. – Igor Sep 11 '12 at 22:35
-
@Igor this implementation of a CellEditor is **invalid** (feels like the 10000th this week ...): it breaks its contract by not notifying listeners on termination for internal reasons – kleopatra Sep 12 '12 at 08:19
-
@kleopatra Could you give a correct one? ..Or at least just edit this one? – Igor Sep 12 '12 at 08:43
-
This doesn't work. When you press return (new line) it should increase the height of the cell. Instead, it switches focus to the next row. Cells are still single lined. – Igor Oct 14 '12 at 20:44
-
@Igor In my opinion that's another question, but well... You can intercept the enter key to prevent it to propagate to the table. – Gilberto Torrezan Oct 14 '12 at 22:34
-
@Gilberto AlthoughI think intercepting enter key won't do it because text can also be pasted into the cell. – Igor Oct 15 '12 at 10:23
-