0

I have a JTable, with editable fields. The following code works:

protected JTable m_table;
protected MyTable m_data;
m_data = new VIPTable(this, title);
m_table = new JTable();
for (int k = 0; k < MyTable.m_columns.length; k++) {
    DefaultTableCellRenderer textRenderer = new DefaultTableCellRenderer();
    textRenderer.setHorizontalAlignment(MyTable.m_columns[k].m_alignment);
    TableCellRenderer renderer = textRenderer;
    TableCellEditor editor;
    if (k==MyTable.COL_CATEGORY)
        editor = new DefaultCellEditor(new JComboBox(MyTable.CATEGORIES));
    else
        editor = new DefaultCellEditor(new JTextField());  // error with JTextArea
    TableColumn column = new TableColumn(k, MyTable.m_columns[k].m_width, 
                              renderer, editor);
    m_table.addColumn(column); 
}

I now want to make the text fields multi-line, so I tried to replace JTextField with JTextArea. But there is no constructor for DefaultCellEditor(JTextArea).

So: how to code a editable multi-line field in a JTable?

Note: I also looked at the solution in PopupEditor, but that has the same problem when I replace JTextField with JTextArea.

Community
  • 1
  • 1
R71
  • 4,283
  • 7
  • 32
  • 60
  • 1
    You can use any class you want as your editor by implementing your own `AbstractCellEditor` as explained in http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editor – DSquare Aug 21 '14 at 09:06
  • not required an AbstractCellEditor, because standard TableCellRenderer has row and column coordinates, the same for TableCellEditor – mKorbel Aug 21 '14 at 10:23
  • @mKorbel: Can you please elaborate how this can be done? If you can show by adapting the full example from Popup, that will help me a lot. Thanks. – R71 Aug 21 '14 at 12:03
  • solution is at [Multiline JTable](http://stackoverflow.com/questions/12378573) – R71 Aug 21 '14 at 13:18
  • @Rog, `I also looked at the solution in PopupEditor, but that has the same problem when I replace JTextField with JTextArea.` - why would you change the JTextField to a JTextArea? The code presented there is a complete solution. There is nothing to change, unless you want to customize the behaviour further. – camickr Aug 21 '14 at 13:55

0 Answers0