I made the following class which contains information about users - name, age photo.
public class User{
private int age;
private String name;
ImageIcon icon;
JLabel image;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public User(){
this.age = 0;
this.name = "";
}
public User(int age, String name){
this.age = age;
this.name = name;
icon = new ImageIcon(name + ".jpg");
image = new JLabel(icon, JLabel.Center);
}
}
I want to create the set of images with users' faces on JPanel. I want to get details of each user after clicking on his photo. So I put this code in the main class:
MouseListener myClick = new MouseListener(){
@Override
public void mouseClicked(MouseEvent e)
{
User selected = new User();
selected.image = (JLabel) e.getComponent();
System.out.println(selected.getAge() + " " + selected.getName());
}
};
and
User[] users = new User[32];
for (int i = 0; i < 32; i++)
{
panel.add(users[i].image);
users[i].image.addMouseListener(myClick);
}
I know- it is totally wrong. I cannot obtain data about the certain user, because I get values from "selected", which contains only JLabel with image (no values for age or name). What should I do to get the name and age of the certain user?