Hello, I ran into a problem that I can’t in any way force the source from the Internet, which, as their authors say "work", make the output in the JTable table multiline.

I took for example the code from here: http://www.sql.ru/forum/707518/mnogostrochnyy-renderer-v-jtable

I add the data in the table through DefaultTableModel.

I apply the source as

table.setDefaultRenderer(String.class, new TextAreaRenderer()); 

For the test, you can take the table from here: http://ru.jakeroid.com/primeryi-ispolzovaniya-jtable.html .

I ask for help, as I have already rummaged everything and did not do this with multi-line output.

example

  • Is it even possible to draw multi-line output in a JTable? - Andrew
  • one
    change in table.setDefaultRenderer(String.class, new TextAreaRenderer()); on Object.class , if using DefaultTableModel or constructor with arrays - zRrr
  • thank you so much for the answer, you helped me a lot - Andrew

1 answer 1

JTable determines the default renderer to be used, not by the actual class of the object in the cell, but by the return value of TableModel.getColumnClass . DefaultTableModel.getColumnClass always returns Object.class , so the renderer assigned to strings is not used.

To solve the problem, you can assign your renderer to Object.class , override the getColumnClass in the model, or set the renderer to the column via TableColumn.setCellRenderer :

 // аргумент getColumn - номер колонки в порядке отображения table.getColumnModel().getColumn( 0 ).setCellRenderer( new TextAreaRenderer() );