Boa tarde a todos. Estou a usar JLabel para visualizar uma imagem carregada a partir de JFileChooser. Para ler a imagem estou a usar BufferedImage, porque quero ler imagens em formato bitmap.
Já desenvolvi o método, mas o problema é que não me aparece a imagem nenhuma no JLabel. Não sei mais por onde procurar o erro. Acho que está a dar o erro na variável leImagem (é um BufferedImage), porque fiz um Toggle Line Breakpoint a esta variável, compilou sem lançar excepção. Peço a vossa ajuda P.F.V.
Em baixo segue o meu código. Já agora uso Netbeans 6.5.
public class RNAInterface extends javax.swing.JFrame {
/** Creates new form RNAInterface */
private File file;
private ImageIcon imagem;
private BufferedImage leImagem;
private JFileChooser carregar;
private javax.swing.JLabel jLabelImagem;
public RNAInterface() {
initComponents();
}
...
private void sairMousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
this.dispose();
}
private void CarregarAssinaturaMousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
JFileChooser jfchooser = new JFileChooser();
//file = new File("user.dir");
file = jfchooser.getSelectedFile();
int option = jfchooser.showOpenDialog(null);
if (option == JFileChooser.APPROVE_OPTION){
try {
//imagem = new ImageIcon(file.getAbsolutePath());
//file.getAbsolutePath();
leImagem = ImageIO.read(file);
imagem = new ImageIcon(leImagem);
int height = imagem.getIconHeight();
int width = imagem.getIconWidth();
jLabelImagem.setSize(width, height);
jLabelImagem.repaint();
jLabelImagem = new JLabel(imagem);
} catch (IOException ex) {
Logger.getLogger(RNAInterface.class.getName()).log(Level.SEVERE, null, ex);
}
}
else{
JOptionPane.showMessageDialog(null,"Voce n\u00e3o selecionou nenhum diretorio.");
}
}
A seguir é a excepção:
run:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1276)
at rna.RNAInterface.CarregarAssinaturaMousePressed(RNAInterface.java:363)
at rna.RNAInterface.access$000(RNAInterface.java:32)
at rna.RNAInterface$1.mousePressed(RNAInterface.java:231)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5514)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3135)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)