-1

basically I am trying to save an image I have edited in a JFrame, so I have a menu with the save item and I have an action listener to set up for the save item and everything works fine, the file chooser comes up and I can select where I would like to save it, only thing is when I hit save, its not there. Here is my code, am I missing something?

 if(e.getSource().equals(Save)){
        JFileChooser keep = new JFileChooser();
        keep.setSelectedFile(new File ("newImage.jpg"));
        FileNameExtensionFilter  filters = new FileNameExtensionFilter("jpeg", "jpg");
        keep.setFileFilter(filters);
        File output = keep.getSelectedFile();

        int count = keep.showSaveDialog(keep);
        BufferedImage out = filteredImage;

        if (count == JFileChooser.CANCEL_OPTION){

        }
        else{
            try{
                ImageIO.write(out, "jpg", output);

                //I put this here to see if I was even reaching the method
                System.out.println("writing method");
            }catch(Exception d){
            }
        }
    }
Elchapo
  • 69
  • 1
  • 3
  • 13
  • 2
    *"am I missing something?" .. `}catch(Exception d){ }`"* information! 1st change that to `}catch(Exception d){ d.printStackTrace(); }` & run the code again. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 22 '16 at 05:25
  • actually I had a JOptionPaneDialog("Your file could not save") but that was printing either, I took it out when I posted it though! Sorry! (I took it out beacuse it orignally said, you just got the d...) EDIT: Ive just added the d.printstacktrace and It didnt print out, so the IMage IO write method is wroking, for some reason its just not showing up where I save it – Elchapo Apr 22 '16 at 05:27
  • Given the number of possible return results from `JFileChooser`, I might suggest you code for the "happy path" first, maybe use something like `if (count == JFileChooser.ACCEPT_OPTION){` instead – MadProgrammer Apr 22 '16 at 05:37
  • Thanks @MadProgrammer Ill check that now EDIT: OKay that didnt change anything, I know for sure the write method is getting called because my syso is printing to the console, its just not appearing on my Desktop – Elchapo Apr 22 '16 at 05:38
  • *"..that didnt change anything,"* Something that might change my interest level (and from that help) is that MCVE. Where is it? – Andrew Thompson Apr 22 '16 at 05:41
  • @Elchapo The suggest was just a "general" suggest, most companies I've worked for like "happy" path coding, keeps the devs on the positive side of things ;) – MadProgrammer Apr 22 '16 at 05:42
  • @MadProgrammer Thanks for the tip! haha hours of java debugging has made me a negative nancy ;) – Elchapo Apr 22 '16 at 05:50

1 Answers1

2

So, you get a reference to the selectedFile...

File output = keep.getSelectedFile();

The you show the dialog...

int count = keep.showSaveDialog(keep);
BufferedImage out = filteredImage;

Then you try and save the image...

ImageIO.write(out, "jpg", output);

...wait, what?! Assuming that getSelectedFile isn't null, how do you know where you're actually saving the image?

That process should be reversed slightly...

showSaveDialog
if (accepted) {
    saveFile = getSelectedFile
    ImageIO.write(img, "jpg", saveFile);
}

as a basic psudo code example

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ive just realized its replacing the orignal image now, it doesnt matter where I try to save it or what I save it as it just replaed the original image, which isnt what I want (nor do I understand why/how that happens) – Elchapo Apr 22 '16 at 05:48
  • You use `getSelectedFile` BEFORE you show the `JFileChooser`, you should be using it AFTER, otherwise you are get what was previously selected, if anything – MadProgrammer Apr 22 '16 at 05:49
  • Thanks! That did it!! – Elchapo Apr 22 '16 at 05:52