Java – how to dynamically set rowheight in JTable
I want to place a string in a JTable that is longer than the given cell width
import javax.swing.*; public class ExampleTable { public JPanel createTable() { JPanel totalGUI = new JPanel(); //define titles for table String[] title = {"TITLE1","TITLE2","TITLE3"}; //table data Object[][] playerdata = { {new Integer(34),"Steve","test test test"},{new Integer(32),"Patrick","dumdi dumdi dummdi dumm di di didumm"},{new Integer(10),"Sarah","blabla bla bla blabla bla bla blabla"},}; //create object 'textTable' JTable textTable = new JTable(playerdata,title); //set column width textTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); textTable.getColumnModel().getColumn(0).setPreferredWidth(60); textTable.getColumnModel().getColumn(1).setPreferredWidth(60); textTable.setDefaultRenderer(String.class,new RowHeightCellRenderer()); //scrollbar JScrollPane scrollPane = new JScrollPane(textTable); totalGUI.add(scrollPane); return totalGUI; } private static void createAndShowGUI() { //create main frame JFrame mainFrame = new JFrame(""); ExampleTable test = new ExampleTable(); JPanel totalGUI = new JPanel(); totalGUI = test.createTable(); //visible mode mainFrame.add(totalGUI); //integrate main panel to main frame mainFrame.pack(); mainFrame.setVisible(true); } public static void main (String[] args) { createAndShowGUI(); }//main }
Here, you will see the code for wrapping each text in a given cell
import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class RowHeightCellRenderer extends JTextArea implements TableCellRenderer { /** * */ private static final long serialVersionUID = 1L; public Component getTableCellRendererComponent (JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column ) { setText( value.toString() ); return this; } }
Thanks, but I want to dynamically implement rowheight, depending on the string length... I want to read the whole string / text in the cell Any suggestions?
I'm a JAVA beginner. This is my first question I'm glad to get the answer
Solution
There are several problems with using jtextarea as a rendering component (and most, if not all, have been explained in several QA's on this site) Try to summarize:
Adjust the height of a single row to the size requirements of the rendering component
Basically, the way to go is to cycle through the cells as needed
>Configure its renderer with data > ask preferred size of rendering components > set table row height to pref height
The updaterowheight method in the problem edited by OP is very good
Jtextarea calculates its preferredSize
In order to get a reasonable size hint, it needs to "sow" in another dimension with some reasonable sizes That is, if we want a height that requires width, it must be done in each call In the context of a table, a reasonable width is the current column width:
public Component getTableCellRendererComponent(JTable table,int column) { ... // configure visuals setText((String) value); setSize(table.getColumnModel().getColumn(column).getWidth(),Short.MAX_VALUE); return this; }// getTableCellRendererComponent
Dynamic height adjustment
The row height is completely determined in some stable states of the table / column / model Therefore, you should set it (call updaterowheight) once after initialization and when any state it depends on changes
// TableModelListener @Override public void tableChanged(TableModelEvent e) { updateRowHeights(); } // TableColumnModelListener @Override public void columnMarginChanged(ChangeEvent e) { updateRowHeights(); }
be careful
As a general rule, all parameters in getxxrendercomponent are strictly read-only, and the implementation must not change any state of the caller It is wrong to update rowheight from the renderer