Duvida recuperar imagem do BD

2 respostas
E
Bom galera, eu uso um Jfilechooser para selecionar a imagem pego essa imagem mostro num Jlabel e salvo o seu endereço no BD MySQL usando o hibernate, isso ai funciona certinho, quando vou carregar a imagem ela também carrega(num Jlabel), mas na tela em que carrega tem um Jbutton para alterar a foto mas não to conseguindo alterar essa imagem alguem pode me ajudar? ta aqui o código onde é salvo o endereço.
objAluno.setFoto(enderecoImagem);// enderecoImagem é a String com o endereço da imagem carregada

  if (objAluno.getFoto()!=null && objAluno.getFoto()!="")
 	    {
 	    	imgLabel.setIcon(new ImageIcon(enderecoImagem));
 	    	atualizarImagem();
 	    	
 	    }
 	    else
 	    {
 	    	objAluno.setFoto("");
 	    }


 public void atualizarImagem()
    {
       ImageIcon imagem = new ImageIcon(enderecoImagem);
        imgLabel.setIcon(new ImageIcon(imagem.getImage().getScaledInstance(imgLabel.getWidth(),imgLabel.getHeight(), Image.SCALE_DEFAULT)));

    }
Aqui ta o método de onde carrega do BD
private void ver ()
    {
        if (jTable2.getSelectedRow()>= 0)
    	{
    		try
    		{
                    alunoVO = new AlunoVO();
                    AlunoVO alunoAux = new AlunoVO();
                    alunoAux.setNome((String) jTable2.getValueAt(jTable2.getSelectedRow(),0));

                    alunoVO = AlunoDAO.selectUniqueNome(alunoAux.getNome());
                    TelaAlunoView alunoView = new TelaAlunoView(alunoVO);
                    TelaPrincipal.deskPane.add(alunoView);
                    alunoView.setVisible(true);
                    alunoView.setLocation((TelaPrincipal.deskPane.getWidth() - alunoView.getWidth())/2, (TelaPrincipal.deskPane.getHeight() - alunoView.getHeight())/2);
                   
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, "Erro! Não Conseguiu atribuir valor do aluno."+e);
                e.printStackTrace();

				
            }
e aqui ta onde eu preencho o label com a foto
imgViewLabel.setIcon( new ImageIcon(alunoVO.getFoto()));


            atualizarImagem();

Agora como eu faço para alterar a imagem do label?

to fazendo assim
private void alterarFotoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarFotoButtonActionPerformed
       TelaCarregarFotoAlunoView telaCarregarFoto = new TelaCarregarFotoAlunoView();
        TelaPrincipal.deskPane.add(telaCarregarFoto);
        telaCarregarFoto.setVisible(true);
        telaCarregarFoto.setLocation((TelaPrincipal.deskPane.getWidth() - telaCarregarFoto.getWidth())/2, (TelaPrincipal.deskPane.getHeight() - telaCarregarFoto.getHeight())/2);
        atualizarImagem();
        telaCarregarFoto.dispose();

        
        
    }
ta aqui a classe do filechooser

public class TelaCarregarFotoAlunoView extends javax.swing.JInternalFrame {

    /** Creates new form TelaCarregarFotoAlunoView */
    public TelaCarregarFotoAlunoView() {
        initComponents();
        
        enderecoImagem = "";
    
    if(FileChooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION){
        TelaCarregarFotoAlunoView.this.dispose();

     }
        enderecoImagem = FileChooser.getSelectedFile().getAbsolutePath();
        TelaCadastrarAluno.enderecoImagem=enderecoImagem;
        TelaAlunoView.enderecoImagem = enderecoImagem;
        dispose();

    }

    public JFileChooser getFileChooser() {
        return FileChooser;
    }

    public void setFileChooser(JFileChooser FileChooser) {
        this.FileChooser = FileChooser;
    }

    @SuppressWarnings("unchecked")
   
    private void initComponents() {

        FileChooser = new javax.swing.JFileChooser();

        setClosable(true);
        setTitle("Alterar Foto");

        FileChooser.setApproveButtonText("Abrir");
        UIManager.put("FileChooser.cancelButtonToolTipText","Cancelar");
        FileChooser.setCurrentDirectory(new java.io.File("C:\"));
        FileChooser.setDialogTitle("Selecionar Foto");
        FileChooser.setAutoscrolls(true);
        FileChooser.setFocusable(false);
        
        FileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionPerformed(evt);
            }
        });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(FileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(FileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(15, Short.MAX_VALUE))
            );
            pack();
        }

    public String getEnderecoImagem() {
        return enderecoImagem;
    }

    public void setEnderecoImagem(String enderecoImagem) {
        this.enderecoImagem = enderecoImagem;
    }

       private String enderecoImagem;
 
    private javax.swing.JFileChooser FileChooser;
  
}

to quebrando a cabeça pra fazer isso se alguem puder dar uma ajuda!

2 Respostas

E

Alguem?

R

primeiro tenha certeza de que a imagem está sendo carregada no Jlabel: após carregar na label, da um getImageIcon nela e um getAbsolutePath no arquivo de imagem pra ver se o caminho que está trazendo ta certo… com isso vc ve tb se o botao ta trabalhando certo.

com essa certeza, após atualizar a imagem da um repaint na tela/painel ou um updateUI ( tente isso após seu método atualizarImagem no local onde a label foi adicionada )

Criado 26 de abril de 2011
Ultima resposta 27 de abr. de 2011
Respostas 2
Participantes 2