文件路径显示取代jtable中的图像

问题描述:

我使用下面的代码在鼠标点击事件显示时显示图像。当我使用下面的代码时,它将显示图像的路径,特别是cell.how以显示图像那个特定的细胞?文件路径显示取代jtable中的图像

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) { 
URL url = getClass().getResource("image/Pointer.GIF"); 
ImageIcon testIcon = new ImageIcon(url); 
jTable1.setValueAt(testIcon, 0, 2); 
} 
+0

关于umpfth时间:只要模型返回正确的列类,JTable就可以很好地处理图标。你打算多久询问一次并得到完全相同的答案? – kleopatra 2012-02-11 13:43:35

+1

downvote是一次又一次询问同一个问题... – kleopatra 2012-02-11 13:44:28

编写您自己的可重用组件并自行管理事件。您将ImageIcon及其路径包装在一个类中。为该类注册一个单击处理程序,并收听点击事件,即更改容器中的组件时的单击事件。保持state变量也交换容器的内容。要显示path即时创建JLabel并将其添加到容器,或者可以在创建组件时创建JLabel

可能你的组件开始喜欢这个

public class MyComponent extends JComponent { 
    private JLabel label; //This displays the path 
    private ImageIcon image; //This displays the image 

    //Create a container of your wish 

    //Attach a click handler to both the label and the image or the container 

    //OnClick swap the JComponent in your container and repaint()! 

    //Construct it like this: 
    MyComponent(String path) { 
     //Initialize JLabel with "text" as the path 
     //Load ImageIcon from the path 
    } 
} 

这仅仅是一个起点。这种方法创建了一个可重用的组件,因此,您可以在JLabel中使用多个“这样”的组件,并且保持代码清洁。

+0

'ClickHandler' ..'onClick'? J2SE有哪些内容? – 2012-02-11 06:12:32

+0

我的意思是让组件实现MouseListener并监听'click'事件。这只是一个抽象的解释。 – 2012-02-11 06:15:18

+0

你可以编辑'ClickHandler'(意味着一个类名)'点击处理程序'(表示用户输入需要..)等?随着最新的编辑,这将是值得加薪的。 – 2012-02-11 06:20:00