JAVA - How To Browse Image File And Display It Using JFileChooser In Java

JAVA-How To Browse Image File And Display It Into JLabel Using JFileChooser In Java

__________________________________________________________________________

In this java Tutorial we will see How To Load A Picture To A JLabel using JFileChooser 
In Java NetBeans .




Project Source Code:


package JavaDB_001;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;



public class Work extends JFrame{
    JButton button ;
    JLabel label;
    
    public Work(){
    super("Set Picture Into A JLabel Using JFileChooser In Java");
    button = new JButton("Browse");
    button.setBounds(300,300,100,40);
    label = new JLabel();
    label.setBounds(10,10,670,250);
    add(button);
    add(label);
    
    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
        
          JFileChooser file = new JFileChooser();
          file.setCurrentDirectory(new File(System.getProperty("user.home")));
          //filter the files
          FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg","gif","png");
          file.addChoosableFileFilter(filter);
          int result = file.showSaveDialog(null);
           //if the user click on save in Jfilechooser
          if(result == JFileChooser.APPROVE_OPTION){
              File selectedFile = file.getSelectedFile();
              String path = selectedFile.getAbsolutePath();
              label.setIcon(ResizeImage(path));
          }
           //if the user click on save in Jfilechooser


          else if(result == JFileChooser.CANCEL_OPTION){
              System.out.println("No File Select");
          }
        }
    });
    
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(700,400);
    setVisible(true);
    }
     
     // Methode to resize imageIcon with the same size of a Jlabel
    public ImageIcon ResizeImage(String ImagePath)
    {
        ImageIcon MyImage = new ImageIcon(ImagePath);
        Image img = MyImage.getImage();
        Image newImg = img.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon image = new ImageIcon(newImg);
        return image;
    }
    
    public static void main(String[] args){
        new Work();
    }
   }

///////////////END
/////OUTPUT:
JFileChooser Step 1
JFileChooser Step 1
Click On Browse
Click On Browse
JFileChooser Click On Save Button
JFileChooser Click On Save Button



Share this

Related Posts

Previous
Next Post »

3 comments

comments
30 août 2017 à 00:43 delete

thanks a lot! your code here is such a great help with my programming studies

Reply
avatar