[英]How to add Balloon Tips to a Cell in JTable,which behave like Tooltips
[英]How to add tooltips to JTable's rows
如何將工具提示添加到JTable的行(Java Swing)? 這些工具提示應包含相對行的相同值。
這是我在我的類中使用的擴展JTable的代碼。 它覆蓋了方法“prepareRenderer”,但是我得到了空單元格,它為行內的每個單元格添加了一個工具提示,而不是整行的一個工具提示(這就是我正在尋找的):
public Component prepareRenderer(TableCellRenderer renderer,int row, int col) {
Component comp = super.prepareRenderer(renderer, row, col);
JComponent jcomp = (JComponent)comp;
if (comp == jcomp) {
jcomp.setToolTipText((String)getValueAt(row, col));
}
return comp;
}
它為行內的每個單元格添加工具提示,而不是整行的一個工具提示
您正在根據行和列更改工具提示。 如果您只希望工具提示按行更改,那么我只會檢查行值並忘記列值。
設置工具提示的另一種方法是覆蓋JTable的getToolTipText(MouseEvent)
方法。 然后,您可以使用表的rowAtPoint(...)
方法獲取行,然后返回該行的相應工具提示。
在創建JTable對象時使用下面的代碼。
JTable auditTable = new JTable(){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
try {
//comment row, exclude heading
if(rowIndex != 0){
tip = getValueAt(rowIndex, colIndex).toString();
}
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
};
請參閱JComponent.setToolTipText()
- 每行數據所需的JComponent 不是表,而是數據的單元格渲染器,可以訪問為每個渲染的單元格配置JComponent。
rowIndex可以是ZERO。
更改:
if(rowIndex != 0){
tip = getValueAt(rowIndex, colIndex).toString();
}
通過:
if(rowIndex >= 0){
tip = getValueAt(rowIndex, colIndex).toString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.