Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement jtable with variable row-height

None of the answers to two previous questions (here and here) resolve my problem.

I have a multi-column jtable for which I want to display string-content of some columns over more than one line within the cell based on newline char's ("\n") within the string. The number of newlines per string is random, known only at run-time. Only the affected row must be adjusted across all columns to the new height. There may be a different number of lines per affected column, and the row-height needs to be adjusted to the maximum height of these, across the columns.

How do I do this?
If possible some sample code would be very much appreciated.
TIA

like image 904
willbeindebtedforever Avatar asked Sep 17 '10 08:09

willbeindebtedforever


People also ask

How do you size a JTable?

TableModel dataModel = new AbstractTableModel() { public int getRowCount() { return rowData. length; } ... // override other method as well } JTable table = new JTable(dataModel); JScrollPane scrollpane = new JScrollPane(table); Simply call the DefaultTableModel#getRowCount() method to get the number of rows.

Can I add buttons in JTable?

We can add or insert a JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.

How do you make a JTable cell editable?

jTableAssignments = new javax. swing. JTable() { public boolean isCellEditable(int rowIndex, int colIndex) { return editable; }};


Video Answer


1 Answers

If I got you right, I think you need a MultilineCellRenderer . There are already plenty of examples around. Normally they are based on a JTextArea to get the line wrap functionality.

I haven't used it myself yet, but here is an example, which looks kinda good at first view:

MultilineCellRenderer

like image 132
crusam Avatar answered Oct 29 '22 04:10

crusam