【问题标题】:file path display Instead of image in jtable文件路径显示而不是jtable中的图像
【发布时间】:2012-02-11 05:55:17
【问题描述】:

我使用以下代码在鼠标单击事件时显示图像。当我使用以下代码时,它将在特定单元格中显示图像的路径。如何在该特定单元格中显示图像?

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);
}

【问题讨论】:

  • 关于无数次:如果模型返回正确的列类,JTable 可以完美地处理图标。您打算多久提出一次并得到完全相同的答案?
  • 反对票是为了一次又一次地问同样的问题......

标签: java image swing jtable mouse


【解决方案1】:

编写您自己的可重用组件并自行管理事件。您将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 中拥有多个“此类”组件而不会出现任何问题并保持代码干净。

【讨论】:

  • ClickHandler .. onClick?什么 J2SE 有这些?
  • 我的意思是让组件实现MouseListener并监听click事件。这只是一个抽象的解释。
  • 您能否将ClickHandler(暗示类名)编辑为“点击处理程序”(表示需要用户输入..)等?这一点,连同最新的编辑,都值得一票。
  • 很抱歉,我主要使用 GWT,这些是我每天都在使用的东西,只是在写我使用的旧框架时有点困惑。
  • 当我们有能力编辑时,无需向 IMO 道歉。好答案。 +1
猜你喜欢
  • 2017-03-14
  • 1970-01-01
  • 2015-08-07
  • 2021-02-05
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 2012-10-21
  • 2011-05-10
相关资源
最近更新 更多