title: Getting a Path from an ImageIcon which is displayed on a JLabel
body: <p>I am building a PhotoViewer. I want to be able to get the path of an ImageIcon which is displayed on a JLabel. Therefore I have created an MouseListener and a FileChooser. I have spent many hours trying to solve this issue.</p>
  
  <pre><code>for (int i=0; i&lt; scaled.size(); i++){
  labels.add(i, new JLabel(new ImageIcon(scaled.get(i))));
  }
  </code></pre>
  
  <p>Here I want the give each Label which is beeing created a name i, so I can getName() later and ask the specific Label which ImageIcon it contains and its path.</p>
  
  <p>I have three ArrayLists:</p>
  
  <pre><code>ArrayList&lt;JLabel&gt; labels = new ArrayList&lt;JLabel&gt;();
  
  ArrayList&lt;ImageIcon&gt; AL = new ArrayList&lt;ImageIcon&gt;();
  
  ArrayList&lt;Image&gt; scaled = new ArrayList&lt;Image&gt;();
  </code></pre>
  
  <p>Now the mouseClicked event should give me a Path to the ImageIcon on the Label which has been clicked.</p>
  
  <pre><code>public void mouseClicked(MouseEvent arg0) {
          System.out.println(arg0.getSource().getName());
          int a = arg0.getSource().getName();
          getImageIcon.labels.get(a);
          CreateFrame(arg0.getSource().getDescription());
      }
  </code></pre>
  
  <p>After that a Frame is created with the Constructot parameter of the Path of the choosen picture. And I will display it in a seperate Frame.</p>
  
  <p>Full Code: </p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.GridLayout;
  import java.awt.Image;
  import java.awt.Label;
  import java.awt.List;
  import java.awt.event.*;
  import java.io.*;
  import java.util.ArrayList;
  import java.util.Arrays;
  import javax.swing.*;
  
  public class ImageViewer {
  
      public static void main(String[] args) {
  
          JFrame frame = new ImageViewerFrame();
          frame.setTitle("Photoviewer");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
      }
  
  }
  
  
  @SuppressWarnings("serial")
  class ImageViewerFrame extends JFrame implements MouseListener{
      JLabel label;
      JFileChooser chooser;
      JMenuBar menubar;
      JMenu menu;
      JMenuItem menuitem;
      JPanel panel = new JPanel();    
  
      public ArrayList&lt;File&gt; images = new ArrayList &lt;File&gt;();
  
      public void method1(){
          JLabel test = labels.get(0);
          System.out.println(test.getName());     
      }
  
  
      ArrayList&lt;JLabel&gt; labels = new ArrayList&lt;JLabel&gt;();
  
      ArrayList&lt;ImageIcon&gt; AL = new ArrayList&lt;ImageIcon&gt;();
  
      ArrayList&lt;Image&gt; scaled = new ArrayList&lt;Image&gt;();
  
      public ImageViewerFrame() {
          setSize(500,500);
          panel.setLayout(new GridLayout(0,5));
          label = new JLabel();
          add(label);
          add(panel);
          JButton test = new JButton ("TEST");
          test.addMouseListener(this);
          panel.add(test);
          panel.setVisible(true);
          chooser = new JFileChooser();
          chooser.setCurrentDirectory(new File("."));
          chooser.setMultiSelectionEnabled(true);
          menubar = new JMenuBar();
          setJMenuBar(menubar);
          menu = new JMenu("File");
          menubar.add(menu);
          menuitem = new JMenuItem("Open");
          menu.add(menuitem);
          menuitem.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent event){
  
                          int result = chooser.showOpenDialog(null);
  
                          if(result == JFileChooser.APPROVE_OPTION) {
  
                              File[] f = chooser.getSelectedFiles();
  
                              for(int i=0; i&lt; f.length; i++)
                              {
                                  images.add(f[i]);
                                  ImageIcon imageicon = new ImageIcon(f[i].toString());
                                  AL.add(imageicon);
  
                              }       
  
                              for (ImageIcon x : AL){
                                  Image image = x.getImage();
                                  Image newimg = image.getScaledInstance(120,120, java.awt.Image.SCALE_SMOOTH);
                                  scaled.add(newimg);
                              }               
  
                              for (int i=0; i&lt; scaled.size(); i++){
                                  labels.add(i, new JLabel(new ImageIcon(scaled.get(i))));
                              }
  
                              for (JLabel x : labels){
                                  x.addMouseListener(ImageViewerFrame.this);
                              }
  
                              for (int i=0; i&lt; scaled.size(); i++){
                                  //panel.add(new JLabel(i), (new ImageIcon (scaled.get(i))));                        
                                  panel.add(labels.get(i));
  
                              }
                                                                  }
                                      }
                              });
  
      }
  
      @Override
      public void mouseClicked(MouseEvent arg0) {
          System.out.println(arg0.getSource().getName());
          int a = arg0.getSource().getName();
          getImageIcon.labels.get(a);
          CreateFrame(arg0.getSource().getDescription());
      }
  
      private void CreateFrame() {
          JFrame frame2 = new JFrame("Test");
          panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
          frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          JPanel panel3 = new JPanel();
          JButton next = new JButton("Next");
          JButton previous = new JButton ("Previous");
          JButton diashow = new JButton ("Diashow");
          panel3.add(next);
          panel3.add(previous);
          panel3.add(diashow);
          panel3.setVisible(true);
          frame2.setVisible(true);
          frame2.add(panel3);
      }
  
  
  
      private void CreateFrame(String s) {
          JFrame frame2 = new JFrame("Test");
          panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
          frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          JPanel panel3 = new JPanel();
          JButton next = new JButton("Next");
          JButton previous = new JButton ("Previous");
          JButton diashow = new JButton ("Diashow");
          panel3.add(next);
          panel3.add(previous);
          panel3.add(diashow);
          panel3.setVisible(true);
          frame2.setVisible(true);
          frame2.add(panel3);
          panel3.add(new JLabel(s));
  
      }
  
      @Override
      public void mouseEntered(MouseEvent arg0) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void mouseExited(MouseEvent arg0) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void mousePressed(MouseEvent arg0) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void mouseReleased(MouseEvent arg0) {
          // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>