可能重复:
如何向 JTable 的行添加工具提示
我想为给定特定列下的单元格上的用户鼠标悬停事件添加工具提示,以便单元格的值完全显示给用户。
我认为覆盖 prepareRenderer() 对于单个单元格可能更容易
JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (c instanceof JComponent) {
if(column == X){
//X is your particlur column number
JComponent jc = (JComponent) c;
jc.setToolTipText(getValueAt(row, column).toString());
}
}
return c;
}
};
只需设置单元格渲染器的工具提示
public class MyCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setToolTipText(...);
return this;
}
}