2010-02-03 59 views
6

当用户在其中输入错误的值时,需要在单元格的上方(或下方)显示工具提示(请参见下图)。 我有一个工具提示,但我需要一个点来显示它在正确的位置,所以我想获得一个单元格的位置。你知道如何得到这个吗?但是,如果你有更好的解决方案来实现这种行为,我愿意接受所有命题(特别是对于工具提示没有与单元格/ Jtable/Panel绑定的事实,并且如果我移动/关闭/ minmize我的窗口中的工具提示是在同一位置显示)在JTable的单元格上方显示工具提示

alt text http://img246.imageshack.us/img246/6756/errorpopup.png

感谢, 达明

回答

1

尝试getCellRect

+0

是啊!这似乎可以回应我的问题,谢谢:) 你知道一个解决方案,工具提示链接到单元格吗? – Damien 2010-02-03 17:40:53

2

你有an example of such feature我n Swing组件视觉指南。

编辑:事实上,它不是一个真正的工具提示,你需要在这里,因为工具提示需要将光标定位在单元格上方。即使光标位于单元格外,您也想显示工具提示,对吗?

无论如何,另一种解决方案是在用户输入的值无效(例如橙色或红色)时更改单元格的背景,然后添加一个“真实”工具提示(使用我提供的链接)为了给用户一个完整的错误信息。

+0

是的,这不是一个“真正的”工具提示。 你的解决方法可能没问题。当用户输入一个值时,我该如何显示一个(真实)工具提示? (我在setValueAt()方法中) – Damien 2010-02-03 17:39:30

6

请参考下面的代码片段中,你会得到解决

JTable table = new JTable() { 
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 
     Component c = super.prepareRenderer(renderer, row, column); 
     if (c instanceof JComponent) { 
      JComponent jc = (JComponent) c; 
      jc.setToolTipText(getValueAt(row, column).toString()); 
     } 
     return c; 
    } 
}; 

如果你想只显示特定的细胞,你需要做的是改变getValueAt的PARAMS列参数( ...)方法,其中包含电池

+0

如果使用RowSorter,则这不起作用,因为组件不会更改顺序,但模型数据将会。 – 2012-11-26 14:59:04

+0

对我来说真的很好(没有RowSorter) – ricgeorge 2013-03-26 10:33:36

0

使用下面的代码,如果使用RowSorter的获得值正确的行特定的列:

jc.setToolTipText(getValueAt(convertRowIndexToModel(row), column).toString()); 
+2

不 - a)为所有单元格设置相同的工具提示b)不解决_above_部分 – kleopatra 2013-01-30 13:49:10

2

只需使用下面的代码,而CR 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; 
      } 
     }; 
相关问题