Atualizar imagem em um jtable em tempo de execuÇÃo

4 respostas
C

Pessoal,

To fazendo uma aplicação swing, onde tenho um jtable com uma jLabel nele, e nesta esta um ImageIcon de uma imagem que está no diretorio da aplicação, só que toda vez que eu troco a imagem por outra de mesmo nome e caminho o jLabel teria que modificar tbm, mas nem com repaint() ele muda.
Caso alguém saiba um obrigada!

...

JFileChooser jfc = new JFileChooser();

        int returnVal = jfc.showDialog(NFe.this, "Ok");

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File file = jfc.getSelectedFile();
                BufferedImage imagem = ImageIO.read(file);
                String extensao = file.getName().substring(file.getName().lastIndexOf("."), file.getName().length());
                String url = getApplicationPath();
                copiaArquivos(file.getAbsolutePath(), url + "\\src\\images\\logoEmitente" + extensao);

                modificarLogoEmitente();


            } catch (IOException ex) {
                Logger.getLogger(NFe.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        jfc.setSelectedFile(null);

    }                                                        

    public void modificarLogoEmitente() {
        
        jPanel7.setLayout(new BorderLayout());
        ImageIcon imagem = new ImageIcon("C:\\(DIRETORIO)\\images\\logoEmitente.jpg");
        
        JLabel label = new JLabel(imagem);
        
        jPanel7.add(label, BorderLayout.CENTER);

        jPanel7.repaint();
    }

4 Respostas

D

Como vc está montado sua tabela??
Posta este código por favor.

C

Me Desculpem, errei ao citar jtable, na verdade é em um JPanel!!

carllacj:
Pessoal,

To fazendo uma aplicação swing, onde tenho um jPanel com uma jLabel nele, e nesta esta um ImageIcon de uma imagem que está no diretorio da aplicação, só que toda vez que eu troco a imagem por outra de mesmo nome e caminho o jLabel teria que modificar tbm, mas nem com repaint() ele muda. Caso alguém saiba um obrigada!
...

JFileChooser jfc = new JFileChooser();

        int returnVal = jfc.showDialog(NFe.this, "Ok");

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File file = jfc.getSelectedFile();
                BufferedImage imagem = ImageIO.read(file);
                String extensao = file.getName().substring(file.getName().lastIndexOf("."), file.getName().length());
                String url = getApplicationPath();
                copiaArquivos(file.getAbsolutePath(), url + "\\src\\images\\logoEmitente" + extensao);

                modificarLogoEmitente();


            } catch (IOException ex) {
                Logger.getLogger(NFe.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        jfc.setSelectedFile(null);

    }                                                        

    public void modificarLogoEmitente() {
        
        jPanel7.setLayout(new BorderLayout());
        ImageIcon imagem = new ImageIcon("C:\\(DIRETORIO)\\images\\logoEmitente.jpg");
        
        JLabel label = new JLabel(imagem);
        
        jPanel7.add(label, BorderLayout.CENTER);

        jPanel7.repaint();
    }
evertonsilvagomesjav

Opa uma coisinha quando vc gera seu .jar ele nao vai pegar seu “src”, cria uma pasta dentro do src com suas imagens e usa no ImageIcon

new ImageIcon(this.getClass().getResource("/icon/suaimagem.png"))

S

Olá carllacj

voê poderia usar dentro do seu projeto uma pasta resources e colocar suas imagens dentro dela.

ImageIcon patrimonioButtonIcon = createImageIcon("resources/kontact_contacts.png");


   btn1.setIcon(patrimonioButtonIcon);


    public static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Frame.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

Silvio Guedes

Criado 9 de março de 2010
Ultima resposta 10 de mar. de 2010
Respostas 4
Participantes 4