Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/09/2010, 18:29
lince_0011
 
Fecha de Ingreso: septiembre-2009
Mensajes: 63
Antigüedad: 14 años, 7 meses
Puntos: 0
JFileChooser e imagenes.

Amigos, tengo un solo form donde puedo insertar imagenes, para ello utilizo el JFileChooser para buscar entre directorios la imagen que necesito.

Al dar clic en el boton de abrir la imagen se pinta automaticamente en un label:

Código PHP:
JFileChooser fileChooser = new JFileChooser();;
        
FileNameExtensionFilter filtro = new FileNameExtensionFilter(".jpg & .gif""jpg""gif");
        
fileChooser.setFileFilter(filtro);
        
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
int result fileChooser.showOpenDialog(this);
        if (
result == JFileChooser.APPROVE_OPTION){
            
File namefileChooser.getSelectedFile();

            
ImageIcon image = new ImageIcon(fileChooser.getSelectedFile().getPath());
            if(
image.getIconHeight() > 342 || image.getIconWidth() > 230){
                
ImageIcon imageScalada = new ImageIcon(image.getImage().getScaledInstance(90100100));
                
label.setIcon(imageScalada);
            }
            else{
                
label.setIcon(image);
            }
        } 
Ahora bien para insertar las imagenes en MySQL hago lo siguiente:

Código PHP:
String sql "INSERT INTO fotos(Foto) VALUES (?)";
PreparedStatement stmt con.prepareStatement(sql);
File imagenBD = new File(fileChooser.getSelectedFile().getPath());
FileInputStream   fis = new FileInputStream(imagenBD);
stmt.setBinaryStream(1fisimagenBD.length());
stmt.execute(); 
Pero tengo una pequeña duda o talvez un problema, esa segunda parte de codigo va todo junto con el del jFileChooser asi:

Código PHP:
JFileChooser fileChooser = new JFileChooser();;
        
FileNameExtensionFilter filtro = new FileNameExtensionFilter(".jpg & .gif""jpg""gif");
        
fileChooser.setFileFilter(filtro);
        
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
int result fileChooser.showOpenDialog(this);
        if (
result == JFileChooser.APPROVE_OPTION){
            
File namefileChooser.getSelectedFile();

            
ImageIcon image = new ImageIcon(fileChooser.getSelectedFile().getPath());
            if(
image.getIconHeight() > 342 || image.getIconWidth() > 230){
                
ImageIcon imageScalada = new ImageIcon(image.getImage().getScaledInstance(90100100));
                
label.setIcon(imageScalada);
            }
            else{
                
label.setIcon(image);
            }
        }


String sql "INSERT INTO fotos(Foto) VALUES (?)";
PreparedStatement stmt con.prepareStatement(sql);
File imagenBD = new File(fileChooser.getSelectedFile().getPath());
FileInputStream   fis = new FileInputStream(imagenBD);
stmt.setBinaryStream(1fisimagenBD.length());
stmt.execute(); 
Entonces lo que pasa es que al momento de abrir la imagen esta se inserta automaticamente en la BD, y eso es obviamente por el query que esta justo despues del codigo para abrir las imagenes.

Lo que yo intento es tener otro boton para insertar la imagen, si me doy a entender? Por ejemplo abro la imagen con el JFileChooser, luego presiono otro boton y la imagen se inserta, asi quiero saber como hacerlo.

Intente colocar el query en el boton de insertar pero las imagenes no se inserta, para lograr insertar las imagenes necesito dejar el codigo tal y como esta
Me podrian ayudar?

Saludos