Save image in variable

0

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())));
    }
    
asked by David Calderon 10.06.2016 в 18:12
source

2 answers

1

For that the most common is to convert the image to bits to save it in a variable of that type and build it again to better understand that option attached to a video where they explain how to save the image plus a text related to that image: link

    
answered by 13.06.2016 / 11:48
source
0

You could do the following, change the scope of your variable icono to a global scope within your class, for example:

public class MiClase {
    private Icon icono;

    //demas variables y metodos de la clase
}

remember then to leave in your method that loads the image the initialization of variable icono

icono= new ImageIcon(icon.getImage()....

In that same class I suppose you must have an instance of some kind that implements ActionListener , so that when you execute the button; execute a certain action.

Since you already have the variable icono initialized (take the time to validate this to avoid possible NullPointerException ) you can pass it as an argument to your other class, either by argument of the same save method or by constructor of your other class.

By argument of your save method:

public class MiClase {
    private MiOtraClase otra;

    //inicializas la variable icono
    icono = .....

    //la pasas como argumento de metodo a otra clase
    otra.guardar(icono);

   //o por constructor
   otra = new MiOtraClase(icono);
}

It is necessary that in your "other class" you define your constructor or your method according to how you are going to store it

//constructor
private Icon icono;

public MiOtraClase(Icon icono) {
    this.icono = icono;
}

Another important topic JAVA attends to the writing style CamelCase , in your code there are variables and names of methods that do not meet with this, instead of VentanaImg_Contacto you must use VentanaImgContacto if it is a class or ventanaImgContacto if it is a variable or a method name. Method names do not start with a capital letter, they are written in lowerCamelCase.

I hope it serves you.

    
answered by 11.06.2016 в 00:39