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.
for (int i=0; i< scaled.size(); i++){
labels.add(i, new JLabel(new ImageIcon(scaled.get(i))));
}
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.
I have three ArrayLists:
ArrayList<JLabel> labels = new ArrayList<JLabel>();
ArrayList<ImageIcon> AL = new ArrayList<ImageIcon>();
ArrayList<Image> scaled = new ArrayList<Image>();
Now the mouseClicked event should give me a Path to the ImageIcon on the Label which has been clicked.
public void mouseClicked(MouseEvent arg0) {
System.out.println(arg0.getSource().getName());
int a = arg0.getSource().getName();
getImageIcon.labels.get(a);
CreateFrame(arg0.getSource().getDescription());
}
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.
Full 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<File> images = new ArrayList <File>();
public void method1(){
JLabel test = labels.get(0);
System.out.println(test.getName());
}
ArrayList<JLabel> labels = new ArrayList<JLabel>();
ArrayList<ImageIcon> AL = new ArrayList<ImageIcon>();
ArrayList<Image> scaled = new ArrayList<Image>();
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< 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< 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< 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
}
}
There are multiple possible approaches to this, none of which involve:
Getting a Path from an ImageIcon..
One way:
File[] f = chooser.getSelectedFiles();
Declare File[] f
as an attribute of the class, so it is accessible later. On click, find the source component, get the image icon, and get the image from it.
JLabel label = (JLabel)mouseEvent.getSource();
ImageIcon imageIcon = (ImageIcon)label.getIcon();
Image img = imageIcon.getImage();
Then:
int index = scaled.get(img);
& the file is:
File file = f[index];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.