Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Identifying text truncation in JTable cells

418 views
Skip to first unread message

Dusan Chromy

unread,
May 9, 2003, 5:18:34 AM5/9/03
to
Hi,

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

Jacob

unread,
May 9, 2003, 6:39:21 AM5/9/03
to

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.

Henrietta Cirnat

unread,
May 11, 2003, 1:14:37 PM5/11/03
to

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


Dusan Chromy

unread,
May 12, 2003, 4:45:59 AM5/12/03
to
Jacob <ja...@yahoo.com> wrote in message news:<3EBB8559...@yahoo.com>...

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

Dusan Chromy

unread,
May 14, 2003, 12:53:30 PM5/14/03
to
Jacob <ja...@yahoo.com> wrote in message news:<3EBB8559...@yahoo.com>...

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;
}

her...@gmail.com

unread,
Jun 22, 2016, 2:35:16 AM6/22/16
to

Thanks for the hints on how to do this. I elaborated a little on it trying to get rid of the TableColumn. I aldready had a subclass of DefaultTableCellRenderer doing some custom coloring in getTableCellRendererComponent(). I could extend this method for doing the tooltip stuff - see this stripped down version:

public class TableCellRendererMy extends DefaultTableCellRenderer implements TableCellRenderer {

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

double cellPrefWidth = getPreferredSize().getWidth();
double cellWidth= table.getCellRect(row, column, false).getWidth();

if (cellPrefWidth>cellWidth) {
try {
setToolTipText(String.valueOf(value));
} catch (Exception e) {
}
}

return this;
}
}

regerds,
stephan
0 new messages