java - Add a method on the x Button on JFrame and WordWrap -
so i'm making notepad on java gui, have lot of problems @ time please me. first problem stated on title want add method x button on jframe. have method on codes below don't know how input on x button.
and other problem word-wrap jcheckboxmenuitem
. not work don't know why . want behave in ordinary notepad (if click it word wrap , if click again stop).
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.scanner; import java.io.*; import javax.swing.text.defaulteditorkit; public class sample extends jframe implements actionlistener { private jtextarea textarea = new jtextarea("", 0,0);// textarea private jmenubar menubar = new jmenubar(); // menubar item private jmenu edit =new jmenu("edit"); // edit menu private jmenu file = new jmenu("file"); // file menu private jmenu format = new jmenu ("format");// format menu // private wordbuttonhandler wordhandler; //in format menu. private jcheckbox word=new jcheckbox("word wrap"); private jmenuitem font=new jmenuitem("font"); // in edit menu. private jmenuitem cut=new jmenuitem(new defaulteditorkit.cutaction()); private jmenuitem copy=new jmenuitem(new defaulteditorkit.copyaction()); private jmenuitem paste=new jmenuitem(new defaulteditorkit.pasteaction()); // in file menu. private jmenuitem saveasfile=new jmenuitem("save as");// save-as option private jmenuitem newfile = new jmenuitem("new"); // new option private jmenuitem printfile = new jmenuitem("print");// print option private jmenuitem openfile = new jmenuitem("open"); // open option private jmenuitem savefile = new jmenuitem("save"); // save option private jmenuitem close = new jmenuitem("close"); // , close option! public sample() { setsize(500, 300); settitle("java notepad"); setdefaultcloseoperation(do_nothing_on_close); textarea.setfont(new font("times new roman", font.bold, 12)); textarea.setautoscrolls(rootpanecheckingenabled); container p = getcontentpane(); p.setlayout(new borderlayout()); p.add(textarea); // menu bar setjmenubar(menubar); menubar.add(file); menubar.add(edit); menubar.add(format); // cut option. cut.setlabel("cut"); cut.setaccelerator(keystroke.getkeystroke(keyevent.vk_x,actionevent.ctrl_mask)); edit.add(cut); // copy option. copy.setlabel("copy"); copy.setaccelerator(keystroke.getkeystroke(keyevent.vk_c,actionevent.ctrl_mask)); edit.add(copy); // paste option. paste.setlabel("paste"); paste.setaccelerator(keystroke.getkeystroke(keyevent.vk_v,actionevent.ctrl_mask)); edit.add(paste); //word wrap option // word.additemlistener(wordhandler); format.add(word); // wordhandler=new wordbuttonhandler(); // font option font.addactionlistener(this); format.add(font); //new option newfile.addactionlistener(this); newfile.setaccelerator(keystroke.getkeystroke(keyevent.vk_n,actionevent.ctrl_mask)); file.add(newfile); // open option openfile.addactionlistener(this); // add action listener (so know when it's been clicked openfile.setaccelerator(keystroke.getkeystroke(keyevent.vk_o,actionevent.ctrl_mask)); // set keyboard shortcut file.add(openfile); // add "file" menu // save option savefile.addactionlistener(this); savefile.setaccelerator(keystroke.getkeystroke(keyevent.vk_s,actionevent.ctrl_mask)); file.add(savefile); // save option. saveasfile.addactionlistener(this); file.add(saveasfile); // print option printfile.addactionlistener(this); printfile.setaccelerator(keystroke.getkeystroke(keyevent.vk_p,actionevent.ctrl_mask)); file.add(printfile); // close option close.setaccelerator(keystroke.getkeystroke(keyevent.vk_f4,actionevent.ctrl_mask)); close.addactionlistener(this); file.add(close); } // public class wordbuttonhandler implements itemlistener{ // // // @override // public void itemstatechanged(itemevent e){ // if(e.getsource()==word){ // if(e.getstatechange()==itemevent.selected) // textarea.setlinewrap(true); // textarea.setwrapstyleword(true); // if(e.getstatechange()==itemevent.deselected) // textarea.setlinewrap(false); // textarea.setwrapstyleword(false); // } // } // } public void windowclosing(windowevent e){ if(textarea.gettext().isempty() == false){ jdialog.setdefaultlookandfeeldecorated(true); int response = joptionpane.showconfirmdialog(null, "do want save changes ?","my notepad",joptionpane.yes_no_option, joptionpane.question_message); if(response == joptionpane.no_option){ system.exit(0); } else if(response == joptionpane.yes_option){ jfilechooser save = new jfilechooser(); int option = save.showsavedialog(this); if (option == jfilechooser.approve_option) { try { bufferedwriter out = new bufferedwriter(new filewriter(save.getselectedfile().getpath())); out.write(this.textarea.gettext()); out.close(); } catch (exception ex) { system.out.println(ex.getmessage()); } } } } else{ system.exit(0); } } @suppresswarnings("empty-statement") public void actionperformed (actionevent e) { if (e.getsource() == this.close) dispose(); if (e.getsource() == this.openfile) { jfilechooser open = new jfilechooser(); int option = open.showopendialog(this); file file ; if (option == jfilechooser.approve_option) this.textarea.settext(""); file=open.getselectedfile(); try{ bufferedreader in = new bufferedreader(new filereader(file)); try{ string line=in.readline(); while(line !=null){ textarea.append(line+"\n"); line=in.readline(); } }catch(ioexception ex){ system.out.println(ex.getmessage()); } } catch (filenotfoundexception ex) { system.out.println(ex.getmessage()); } } else if (e.getsource() == this.savefile || e.getsource ()==this.saveasfile) { jfilechooser save = new jfilechooser(); int option = save.showsavedialog(this); if (option == jfilechooser.approve_option) { try { bufferedwriter out = new bufferedwriter(new filewriter(save.getselectedfile().getpath())); out.write(this.textarea.gettext()); out.close(); } catch (exception ex) { system.out.println(ex.getmessage()); } } } else if(e.getsource()==this.printfile){ try { boolean complete = textarea.print(); if (complete) { /* show success message */ joptionpane.showmessagedialog(null,"success","printing",joptionpane.information_message); } else { /*show message indicating printing cancelled */ joptionpane.showmessagedialog(null,"cancelled","printing",joptionpane.warning_message); } } catch (exception pe) { /* printing failed, report user */ joptionpane.showmessagedialog(null,"failed","printing",joptionpane.error_message); } } else if (e.getsource()==this.newfile){ if(textarea.gettext().isempty() == false){ // textarea.settext(""); jdialog.setdefaultlookandfeeldecorated(true); int response = joptionpane.showconfirmdialog(null, "do want save changes ?","my notepad",joptionpane.yes_no_option, joptionpane.question_message); if(response == joptionpane.no_option){ textarea.settext(""); } else if(response == joptionpane.yes_option){ jfilechooser save = new jfilechooser(); int option = save.showsavedialog(this); if (option == jfilechooser.approve_option) { try { bufferedwriter out = new bufferedwriter(new filewriter(save.getselectedfile().getpath())); out.write(this.textarea.gettext()); out.close(); textarea.settext(""); } catch (exception ex) { system.out.println(ex.getmessage()); } } } } } } public static void main(string args[]) { sample app = new sample(); app.setvisible(true); } }
how can make block of code work. in codes above? can't seem make run
public void windowclosing(windowevent e){ if(textarea.gettext().isempty() == false){ jdialog.setdefaultlookandfeeldecorated(true); int response = joptionpane.showconfirmdialog(null, "do want save changes ?","my notepad",joptionpane.yes_no_option, joptionpane.question_message); if(response == joptionpane.no_option){ system.exit(0); } else if(response == joptionpane.yes_option){ jfilechooser save = new jfilechooser(); int option = save.showsavedialog(this); if (option == jfilechooser.approve_option) { try { bufferedwriter out = new bufferedwriter(new filewriter(save.getselectedfile().getpath())); out.write(this.textarea.gettext()); out.close(); } catch (exception ex) { system.out.println(ex.getmessage()); } } } } else{ system.exit(0); } }
add method on x button on
jframe
add windowlistener
. see how write window listeners details , examples.
Comments
Post a Comment