如何在单击行时在鼠标附近显示图像

时间:2012-02-25 16:27:43

标签: java image swing jtable

我的代码看起来像那样

public ImageIcon pictures[]=new ImageIcon[100];
jLabel10.setIcon(pictures[jTable1.getSelectedRow()]);  
jLabel10.setLocation(getMousePosition().x,getMousePosition().y);

如何在点击事件上显示鼠标指针附近的图像(我的意思是在jTable中)?

1 个答案:

答案 0 :(得分:2)

制作标签的默认配置,假设您在3x3表格中有9个标签,这些标签有9个图像路径:

JLabel[][] labels = new JLabel[3][3];
String[][] paths = new String[3][3];

在MouseListener实现中,您可以将一些文本附加到单击的标签上以显示图像:

table.addMouseListener(new MouseAdapter()
{
    public void mouseClicked(MouseEvent e)
    {
       int row = jTable.rowAtPoint(e.getPoint());
       int col = jTable.columnAtPoint(e.getPoint());
       // Assuming you have initialized the labels array and paths array.
       labels[row][col].setText(labels[row][col].getText() 
                                + "<html><img src=\""
                                + YourClass.class.getResource(paths[row][col])
                                + "\">);        
    }
}