2013-03-14 90 views
0

我有一个带有自定义单元格渲染器的JTable。我添加了一个带有多个JLabel的面板给JTable。问题是,当鼠标悬停在每个JLabel上时,我似乎无法获得每个工具的提示。我不想设置jpanel工具提示,我想显示我在jpanel内的3个JLabel上设置的工具提示。JTable中JPanel中的JLabel工具提示不起作用

继承人我的JTable初始化:

public static JTable users = new JTable(model) { 
    @Override 
    public boolean isCellEditable(int row, int column) { 
     return false; 

    } 

    @Override 
    public Class<? extends Object> getColumnClass(int column) { 
     return getValueAt(0, column).getClass(); 
    } 

    @Override 
    public String getToolTipText(MouseEvent event) { 
     String tip = null; 
     Point p = event.getPoint(); 

     // Locate the renderer under the event location 
     int hitColumnIndex = columnAtPoint(p); 
     int hitRowIndex = rowAtPoint(p); 

     if (hitColumnIndex != -1 && hitRowIndex != -1) { 
      TableCellRenderer renderer = getCellRenderer(hitRowIndex, hitColumnIndex); 
      Component component = prepareRenderer(renderer, hitRowIndex, hitColumnIndex); 
      Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false); 
      component.setBounds(cellRect); 
      component.validate(); 
      component.doLayout(); 
      p.translate(-cellRect.x, -cellRect.y); 
      Component comp = component.getComponentAt(p); 
      if (comp instanceof JComponent) { 
       return ((JComponent) comp).getToolTipText(); 
      } 
     } 

     // No tip from the renderer get our own tip 
     if (tip == null) { 
      tip = getToolTipText(); 
     } 

     return tip; 
    } 
}; 

和我的自定义单元格渲染代码:

public class TransparentRenderer extends DefaultTableCellRenderer implements TableCellRenderer { 

public JPanel p = new JPanel(); 

    ........ 

// This method is called each time a cell in a column 
// using this renderer needs to be rendered. 
@Override 
public Component getTableCellRendererComponent(JTable table, Object value, 
     boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) { 

    .... 


    // Now lets color in the table accordingly.. 
    if (value.toString().equals(gmeLobby.user)) { 
     JLabel l = new JLabel(value.toString()); 
     l.setForeground(Color.YELLOW); 
     p.add(l); 

     //setHorizontalAlignment(SwingConstants.LEFT); 
    } else { 
     // If username is not yours 
     //setHorizontalAlignment(SwingConstants.LEFT); 

     if(value instanceof ImageIcon) 
     { 
      final JLabel l = new JLabel(); 
      l.setIcon((ImageIcon) value); 
      l.setAlignmentY(0.85f); 
      l.setToolTipText(((ImageIcon) value).getDescription()); 
      p.add(l); 

      if(vColIndex==0) 
      { 
      final JLabel w = new JLabel(); 
      final JLabel d = new JLabel(); 
      try { 
       w.setIcon(gmeImages.gmeImages("globe")); 
       w.setToolTipText(((ImageIcon) value).getDescription()); 
       d.setIcon(gmeImages.gmeImages("dnds")); 
       d.setToolTipText(((ImageIcon) value).getDescription()); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      p.add(d); 
      p.add(w); 
      setPosition(w, 5, 0); 
      setPosition(d, 15, 2); 
      } 
      setPosition(l, 0, 0); 

     }else{ 
      JLabel l = new JLabel(value.toString()); 
      l.setForeground(Color.WHITE); 
      p.add(l); 

     } 
     return p; 
    } 

    if (isSelected) { 
     setBackground(new Color(rsbc.getRed() - 100, rsbc.getGreen(), rsbc.getBlue())); 
    } else { 
     setBackground(bgu); 
    } 

    if (hasFocus) { 
     // this cell is the anchor and the table has the focus 
     p.setBackground(new Color(rsbc.getRed() - 100, rsbc.getGreen(), rsbc.getBlue())); 
    } 


    // Since the renderer is a component, return itself 

    return this; 
} 

任何帮助将是巨大的。工具提示的JTable上的当前覆盖似乎只是获得了添加的第一个组件的工具提示。

感谢

编辑:

谢谢大家谁是帮助,同时使SSCCE为安德烈评论,我似乎得到问题的固定,而不是确定错误是什么exacly,我认为这可能与我初始化imageicon变量的方式有关。

反正在每个图像上的提示小区2个imageicons一个示例程序如下:

import java.awt.*; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.*; 
import javax.swing.border.MatteBorder; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellRenderer; 
import javax.swing.table.TableColumn; 
import javax.swing.text.BadLocationException; 

@SuppressWarnings("serial") 
public class design extends JFrame{ 

// Create the JTable users list and set editable to false 
public static DefaultTableModel model = new DefaultTableModel(); 
    public static JTable users = new JTable(model) { 
     @Override 
     public boolean isCellEditable(int row, int column) { 
      return false; 

     } 

     @Override 
     public Class<? extends Object> getColumnClass(int column) { 
      return getValueAt(0, column).getClass(); 
     } 

     @Override 
     public String getToolTipText(MouseEvent event) { 
      String tip = null; 
      Point p = event.getPoint(); 

      // Locate the renderer under the event location 
      int hitColumnIndex = columnAtPoint(p); 
      int hitRowIndex = rowAtPoint(p); 

      if (hitColumnIndex != -1 && hitRowIndex != -1) { 
       TableCellRenderer renderer = getCellRenderer(hitRowIndex, hitColumnIndex); 
       Component component = prepareRenderer(renderer, hitRowIndex, hitColumnIndex); 
       Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false); 
       component.setBounds(cellRect); 
       component.validate(); 
       component.doLayout(); 
       p.translate(-cellRect.x, -cellRect.y); 
       Component comp = component.getComponentAt(p); 
       if (comp instanceof JComponent) { 
        return ((JComponent) comp).getToolTipText(); 
       } 
      } 

      // No tip from the renderer get our own tip 
      if (tip == null) { 
       tip = getToolTipText(); 
      } 

      return tip; 
     } 
    }; 

/** 
* @param args 
* @throws Exception 
*/ 
public design() throws Exception 
{ 
    initialize(); 

    setSize(400, 400); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    add(users); 
    setVisible(true); 
} 

public static void main(String[] args) throws Exception { 
    // TODO Auto-generated method stub 
    new design(); 
} 

public static void initialize() throws Exception 
{ 

    model.addColumn("icons"); 
    model.addColumn("text"); 

    users.setRowHeight(40); 

    TableColumn col = users.getColumnModel().getColumn(0); 
    col.setCellRenderer(new TransparentRenderer()); 
    TableColumn col2 = users.getColumnModel().getColumn(1); 
    col2.setCellRenderer(new TransparentRenderer()); 

    model.addRow(new Object[] { new ImageIcon(), "text here" }); 
} 

    } 

class TransparentRenderer extends JPanel implements TableCellRenderer { 
private static final long serialVersionUID = 1L; 
public JPanel p = new JPanel(); 
private static SpringLayout spring = new SpringLayout(); 

public TransparentRenderer() throws Exception, BadLocationException {p.setLayout(spring);} 

// This method is called each time a cell in a column 
// using this renderer needs to be rendered. 
@Override 
public Component getTableCellRendererComponent(JTable table, Object value, 
     boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)  { 

     if(value instanceof ImageIcon) 
     {    
      if(vColIndex==0) 
      { 
      JLabel w = new JLabel(); 
      JLabel d = new JLabel(); 
      try { 
       ImageIcon icon1 = new ImageIcon(); 
       extracted("http://fxtrade.oanda.com/wandacache/star_icon-c10fffd09c7a7548f329f56e446f3cfe5463558b.png" 
         , icon1, "Tooltip for icon 1"); 

       ImageIcon icon2 = new ImageIcon(); 
       extracted("http://hotels.online.com.sg/DB/icon/star_icon2.gif" 
         , icon2, "Tooltip for icon 2"); 

       w.setIcon(icon1); 
       w.setToolTipText("ToolTip 1"); 
       d.setIcon(icon2); 
       d.setToolTipText("ToolTip 2"); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      p.add(d); 
      p.add(w); 
      setPosition(d, 5, 2); 
      setPosition(w, 5, 0); 
      } 

      return p; 
     }else{ 
      add(new JLabel(value.toString())); 
     }  
    return this; 
} 

public void setPosition(JComponent c, int w, int n) 
{ 
    spring.putConstraint(SpringLayout.WEST, c, w, SpringLayout.WEST, p); 
    spring.putConstraint(SpringLayout.NORTH, c, n, SpringLayout.NORTH, p); 
} 

public void extracted(String link, ImageIcon icon, String desc) throws Exception 
{ 
    BufferedImage image = ImageIO.read(new URL(link)); 
    icon.setImage(image); 

    if(desc.equals("")){} else{ icon.setDescription(desc);} 
} 
} 

感谢所有谁帮助:)

+0

为了更快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2013-03-14 11:11:58

回答

0

请问你的代码在所有的工作? TransparentRenderer是单列每列,并且每行逐行调用getTableCellRendererComponent。看起来每个细胞将被渲染一个新的孩子被添加到变量p。这可能是你总是只看到第一个组件的原因。

+0

如果'value.toString()。equals(gmeLobby.user)'是'true',你应该检查你的代码是否工作:在这种情况下,它返回'this',它没有父子关系到面板'p '。 – Claude 2013-03-14 09:33:15

+0

http://oi46.tinypic.com/2m4w2gx.jpg 首先感谢您的快速回复,这是工作渲染器的当前图片,我也删除了变量p,并使transparentRenderer类扩展了JPanel。然而,它仍然不起作用,在这张图片中,你会看到第一个列表中的3个JLabel,其中我试图获取工具提示。希望这能进一步帮助你迈克尔。 – user2168999 2013-03-14 09:36:47

+0

请尝试使用表格中的多个条目并有时更改选择。对于工具提示问题:如果您在'p'上设置tootip文本并使用'((JComponent)组件).getToolTipText()'作为回退,它会起作用吗? – Claude 2013-03-14 09:44:03