简体   繁体   中英

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?

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();
 }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM