java - Saving a JPanel in to png file -
i'm trying implement payslip generator. got gui generate payslip on separate jpanel
. when click "save png" button saving jpanel png file inside project folder. want able specify both file path , file name when saving. here have done far.
public static bufferedimage getscreenshot(component component) { bufferedimage image = new bufferedimage(component.getwidth(), component.getheight(), bufferedimage.type_int_rgb); component.paint(image.getgraphics()); return image; } public void savescreenshot(component component, string fname) throws exception { bufferedimage img = getscreenshot(component); imageio.write(img, "png", new file(fname)); } private void saveaspngbuttonactionperformed(java.awt.event.actionevent evt) { try { savescreenshot(payslip, "my panel image.png"); } catch (exception e) { } }
i want code below. 1 i'm using save searched results jtable
text file.
private void export2textactionperformed(java.awt.event.actionevent evt) { try { jfilechooser fc = new jfilechooser(); int option = fc.showsavedialog(searchemployeegui.this); if (option == jfilechooser.approve_option) { try { string filename = fc.getselectedfile().getname(); string path = fc.getselectedfile().getparentfile().getpath(); int len = filename.length(); string ext = ""; string file = ""; if (len > 4) { ext = filename.substring(len - 4, len); } if (ext.equals(".txt")) { file = path + "\\" + filename; } else { file = path + "\\" + filename + ".txt"; } filewriter fw = new filewriter(file); bufferedwriter bw = new bufferedwriter(fw); bw.write("employee id first name last name gender contact no email date of join designation basic salary"); bw.newline(); bw.write("--------------------------------------------------------------------------------------------------------------------------------------------"); bw.newline(); (int = 0; < employeetable.getrowcount(); i++) { (int j = 0; j < employeetable.getcolumncount(); j++) { bw.write(employeetable.getmodel().getvalueat(i, j) + " "); } bw.newline(); } bw.close(); fw.close(); int answer = joptionpane.showconfirmdialog(null, "would open exported file?", "successfully exported!", option); if (answer == joptionpane.yes_option) { try { desktop dt = desktop.getdesktop(); dt.open(new file(file)); } catch (exception e) { joptionpane.showmessagedialog(null, e); } } } catch (exception e) { joptionpane.showmessagedialog(null, e); } } } catch (exception e) { joptionpane.showmessagedialog(null, e); } }
in here can specify path , file name. want same thing "save png"
button. have no idea how that. can please help? in advance.
you can use jfilechooser
string suggesteddir = "."; string extension = ".png"; jfilechooser filechooser = new jfilechooser(suggesteddir); jframe choose = new jframe(); choose.settitle("save ..."); int status = filechooser.showsavedialog(choose); if (status == jfilechooser.approve_option) { try { file selectedfile = filechooser.getselectedfile(); string newfile = selectedfile.getcanonicalpath(); if (!newfile.endswith(extension)) { newfile=newfile + extension; } imageio.write(img, "png", new file(newfile)); //write img file } catch (ioexception ex) { ex.printstacktrace(); } }
Comments
Post a Comment