Pick up a directory by JFilechooser and save Itext Report in .pdf to that directory in Java -
i created forms backup function netbeans.
when select date jdaychooser(date validation included in code)and click ok button, jfilechooser open asking saving directory.(shown in screenshot below).
i want below functionality done ;
when click ok,itext generated report must save selected directory jfilchooser..
i coded not 100% working (it's saving pdf in project included directory )
help me correct code...
method ok button action performed ;
private void backupokactionperformed(java.awt.event.actionevent evt) { int result; date nowdate = new date(system.currenttimemillis()); date backday = backup_date.getdate(); if(backday==null){//checking date null or not joptionpane.showmessagedialog(null, "please enter date ..."); }else if(!backday.before(nowdate)){//checking given date before todays date or not joptionpane.showmessagedialog(null, "please enter date before todays date..."); }else{ // backup function goes here chooser = new jfilechooser(); // chooser.setdialogtype(jfilechooser.save_dialog); chooser.setcurrentdirectory(new java.io.file(".")); chooser.setdialogtitle("save backup"); chooser.setapprovebuttontext("save"); //disables filesoptioning here chooser.setfileselectionmode(jfilechooser.directories_only); chooser.setacceptallfilefilterused(false); if(chooser.showopendialog(this)==jfilechooser.approve_option){ // system.out.println("getcurrentdirectory(): "+ chooser.getcurrentdirectory()); // system.out.print("getselectedfile() : "+chooser.getselectedfile()); // creating pdf supplier details try { document pdfsup = new document(); pdfwriter.getinstance(pdfsup, new fileoutputstream("supplier details report.pdf")); pdfsup.open(); image imgsup = image.getinstance("hedder.png"); // pdfsup.add(new paragraph("suppliers")); pdfsup.add(imgsup); pdfsup.add(new paragraph("supplier details report",fontfactory.getfont(fontfactory.times_bold, 18, font.bold, basecolor.blue))); pdfsup.add(new paragraph(new date().tostring())); pdfsup.add(new paragraph("----------------------------------------------------------------------------------------------------------------")); pdfptable tablesup= new pdfptable(2); pdfpcell cell = new pdfpcell(new paragraph("title")); cell.setcolspan(4); cell.sethorizontalalignment(element.align_center); cell.setbackgroundcolor(basecolor.pink); tablesup.addcell(cell); tablesup.addcell("supplier id"); tablesup.addcell("supplier id2"); tablesup.addcell("supplier id3"); tablesup.addcell("supplier id4"); pdfsup.add(tablesup); pdfsup.close(); joptionpane.showmessagedialog(null, "report saved..."); } catch (documentexception ex) { logger.getlogger(backup.class.getname()).log(level.severe, null, ex); } catch (filenotfoundexception ex) { logger.getlogger(backup.class.getname()).log(level.severe, null, ex); } catch (ioexception ex) { logger.getlogger(backup.class.getname()).log(level.severe, null, ex); } }else{ system.out.println("no selection"); } } }
new fileoutputstream("supplier details report.pdf")
create reference file in current working directory, has no path information associated with, you've ignored jfilechooser
had provided.
consider using more like...
pdfwriter.getinstance(pdfsup, new fileoutputstream(new file(chooser.getselectedfile(), "supplier details report.pdf")));
which uses selectedfile
jfilechooser
, assuming you've allowed directory selection
Comments
Post a Comment