alexander-r
2/6/2017 - 4:51 PM

Wrap text within table cell

Wrap text within table cell

//  String should be HTML with body style defining max width
diffString = "<html><body style='width: 120px'>" + mzFormat.format(diff) + "<b><font color=\"blue\"> (" + StringUtils.join(losses, " or ") + ")</font></b></html>";

static class WordWrapCellRenderer extends JLabel implements TableCellRenderer {

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		
		setText(value.toString());
		validate();
		setSize(table.getColumnModel().getColumn(column).getWidth(), getPreferredSize().height);
		
		if (table.getRowHeight(row) < getPreferredSize().height)
			table.setRowHeight(row, getPreferredSize().height);
		
		return this;
	}
}