I have this code to add an image to JLabel
:
public void Cargar_Imagen() {
File fichero;
int resultado;
VentanaImg_Contacto vi= new VentanaImg_Contacto();
FileNameExtensionFilter filtro=
new FileNameExtensionFilter("JPG y PNG", "jpg", "png");
vi.jfchCargarFoto.setFileFilter(filtro);
resultado= vi.jfchCargarFoto.showOpenDialog(null);
if(JFileChooser.APPROVE_OPTION== resultado)
{
fichero= vi.jfchCargarFoto.getSelectedFile();
try {
ImageIcon icon= new ImageIcon(fichero.toString());
Icon icono= new ImageIcon(icon.getImage().
getScaledInstance(VentanaAgr_Contacto1.lblFoto.getWidth(),
VentanaAgr_Contacto1.lblFoto.getHeight(), Image.SCALE_DEFAULT));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error al abrir la imagen "+e);
}
}else if(JFileChooser.CANCEL_SELECTION == null ? Integer.toString(resultado) == null : JFileChooser.CANCEL_SELECTION.equals(Integer.toString(resultado)))
{
VentanaImg_Contacto.jfchCargarFoto.setVisible(false);
}
}
I would like to know how I can do for when I press a button the selected image, it is saved in a private variable of another class where I have this code:
public void Guardar(){
arreglo.add(new Controlador.Contacto(
// Aquí debería poner el nombre del JLabel junto con setIcon no es compatible,
txt_Nombre.getText(), txt_Apellido.getText(),
Integer.parseInt(txt_Movil.getText()),
Integer.parseInt(txt_Casa.getText())));
}