PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 7, 2022

[FIXED] How to save an image using JFileChooser

 May 07, 2022     image, java, jfilechooser, serialization, swing     No comments   

Issue

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


Answered By - user14410420
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing