I have a JTable which contains text cells.
I want to have pretty general feature - if text doesn't fit in cell I want to display it in tooltip.
Should I use TableCellRenderer
for th开发者_运维百科at or such simple feature may be done easily (probably just setting some flag?)
also I need to gray-out non-editable cells.
you should use a ToolTip, either a specialized one like Netbeans' (I think that's the origin) Viewtooltips with its dedicated manager (following link contains a reference to the original)
http://javabyexample.wisdomplug.com/java-concepts/34-core-java/59-tips-and-tricks-for-jtree-jlist-and-jcombobox-part-i.html
or re-use the normal tooltip mechanism, this involves: - measuring the size-requirement of the cell by configuring its renderer, and comparing the rendering component's pref width with the actual column width, if necessary, set the tooltip accordingly - some logic in a JTable subclass to detect if that special tooltip is necessary and position at the upper leading corner
Edit (second part of question, config color of not-editable cells)
same way as every configuration of the rendering component: in a custom renderer, basically
// we are in getXXRenderingComponent
if (table.isCellEditable(row, column)) {
setForeground(normalColor);
} else {
setForeground(notEditableColor);
}
Or use SwingX (can't resist, can I :)
Highlighter hl = new ColorHighlighter(HighlightPredicate.READ_ONLY,
null, notEditableColor);
table.addHighlighter(hl);
精彩评论