javaimageswingserializationjfilechooser

How to save an image using JFileChooser


Background info: I've made a program that uploads an image using JFileChooser and have made fill in the space of the JFrame.

My Question: I've attempted implementing my method on a save button and so far I can pull up the JFileChooser but it will not actually save the image. So how would I go about saving the same image I previously uploaded using JFileChooser? I've seen many examples but don't really understand them.

My browse img code:

// When button pressed, allows user to browse inventory
BrowseButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JFileChooser file = new JFileChooser();
        file.setCurrentDirectory(new File(System.getProperty("user.home")));
        // Filter files
        FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg",
                "png");
        file.addChoosableFileFilter(filter);
        int res = file.showSaveDialog(null);
        if(res == JFileChooser.APPROVE_OPTION) {
            File selFile = file.getSelectedFile();
            String path = selFile.getAbsolutePath();
            label.setIcon(resize(path));
        } // End if
    } // End actionPerformer
}); // End ActionListener

My save img code:

// Save file
saveButton.addActionListener(new ActionListener() {    
    public void actionPerformed(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileFilter(new FileNameExtensionFilter("*.png", "png"));
            if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                File file = fileChooser.getSelectedFile();
                String img = file.getAbsolutePath();
            } // End if
        } // End actionPerformed
    }); // End ActionListener

Updated save img code:

    // Save getFile
    saveButton.addActionListener(new ActionListener() {    
        public void actionPerformed(ActionEvent e) {
            JFileChooser saveFile = new JFileChooser();
            saveFile.setCurrentDirectory(new File(System.getProperty("user.home")));
            // Filter files
            FileNameExtensionFilter filter2 = new FileNameExtensionFilter(".Images", "jpg", "png");
            saveFile.addChoosableFileFilter(filter2);
            int f1 = saveFile.showSaveDialog(null);
            if(f1 == JFileChooser.APPROVE_OPTION) {
                File file = saveFile.getSelectedFile();
                    try {
                        ImageIO.write(selFile, "png", file);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } // End if
            } // End actionPerformed
        }); // End ActionListener
            

Solution

  • BufferedImage getImg;
    private String path1;  
    private final JButton saveButton = new JButton("Save");  
    
    saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser getFile = new JFileChooser();
                getFile.setCurrentDirectory(new File(System.getProperty("user.home")));
                // Filter files
                FileNameExtensionFilter filter1 = new FileNameExtensionFilter("*.Images", "jpg",
                        "png");
                getFile.addChoosableFileFilter(filter1);
                int res = getFile.showSaveDialog(null);
                if(res == JFileChooser.APPROVE_OPTION) {
                    selFile1 = getFile.getSelectedFile();
                    path1 = selFile1.getAbsolutePath();
                    label.setIcon(resize(path1));
                    System.out.println("1st selFile1 = " + selFile1);                    
                    try {
                        ImageIO.write(getImg, "jpg", selFile1);
                    } catch (IOException ex) {
                    }
                } // End if
            } // End actionPerformer
        }); // End ActionListener