Hello everyone.
1)I am trying several days now to display an image from file Chooser in Netbeans. i have read and tried many different approaches from the web, but something goes wrong.Although the program runs ok,I see no results.Until now, I have used the "Design" tab of Netbeans to create one main Jframe and three Panels.
Panel1 for image preview(of course I will implement it much later)
Panel2 for buttons like "Add pic","remove" etc.The Add button opens my file Chooser.Now I am trying to load an image from the file Chooser inside Panel3 and of course fit it correctly according to Panel3's size.
The code for Add Button action is something like this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
File file;
BufferedImage image=null;
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
// myfileFilter filter = new myfileFilter();
// fileChooser.addChoosableFileFilter(filter);
if (returnValue == JFileChooser.APPROVE_OPTION){
System.out.println("Ok");
try{
file=fileChooser.getSelectedFile();
System.out.println("Selected file is"+ file);
image=ImageIO.read(file);
System.out.println("Ok");
ImageIcon icon = new ImageIcon(image);
JLabel picLabel = new JLabel(icon);
// picLabel.setIcon(icon);
//picLabel.revalidate();
// picLabel.repaint();
// JLabel picLabel = new JLabel(new ImageIcon(image));
System.out.println("Ok");
//picLabel.setIcon(icon);
jPanel3.add(picLabel);
jPanel3.invalidate();
jPanel3.repaint();
// int w = image.getWidth(null);
// int h = image.getHeight(null);
// BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// Graphics g = bi.getGraphics();
// g.drawImage(image,0,0,null);
// System.out.println("Ok");
}catch(IOException e){
}}
}
As you can see I included all comment lines just to see what else I have tried.When I run the file,file Chooser opens but when I try to open a .jpg file,nothing happens.
2)Also,I cannot implement a file filter,any ideas on that?Maybe in order to load the image some other action outside the "private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)" function(as it was named by Netbeans),is required?
3)Finally I would like a short explanation about what "listener" is(like actionListener etc).I have seen it many times but I am a little bit confused about what it represents and what it does....