if a text in a JTable cell is too wide to fit in, it gets the elipsis
"..."
at the end and gets truncated.
Is there any way to tell whether this has happened in a particular
cell?
I wrote a table cell renderer that shows a tool tip text with the
cell's value, now I'd like to limit the renderer to show the tool tip
for those truncated cells only.
Thanks,
Dusan Chromy
You can compare the width of the column with the width
of the text (using class FontMetrics), but I assume you
maybe want a more explicit approach.
Hi,
i had the same problem! JTable has an attribute autoResizeMode. In default
state it is set to JTable.AUTO_RESIZE_ALL_COLUMNS. In this case JTable
simply truncates everything to the "right"size. I have set this mode to
JTable.AUTO_RESIZE_OFF.
I know, this is not an answer to your question above, but maybe it can be
useful to you, if you want to avoid this problem.
Henrietta
Well, I was kinda hoping there was an easier way to achieve it (a less
explicit approach :-), but one eventuallly has to do the dirty work...
I am now busy with other parts of the graphical interface, but I hope
to have time to return to this later. When/If I get the FontMetrics
solution working, I'll post it here.
Regards,
Dusan
OK, I finally got it working. Subclassing DefaultTableCellRenderer
does the trick (the class included below). You need a TableColumn
object, then you use it like this:
TableColumn tc = ...
tc.setCellRenderer( new ToolTipRenderer(tc) );
Here comes the code:
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
/**
*
* @author chromy
* @version
*/
public class ToolTipRenderer extends DefaultTableCellRenderer {
public ToolTipRenderer(TableColumn col) {
m_col = col;
}
protected void setValue(Object value) {
super.setValue(value);
int cellWidth = getPreferredSize().width;
int colWidth = m_col.getWidth();
setToolTipText(null); // fall-back value first
if (colWidth <= cellWidth)
try {
setToolTipText((String)value);
} catch (Exception e) {}
}
private TableColumn m_col;
}