File selector jfilechooser Creation

Source: Internet
Author: User
Package src10; import Java. AWT. borderlayout; import Java. AWT. container; import Java. AWT. dimension; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. windowadapter; import Java. AWT. event. using wevent; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import javax. swing. Jbutton; import javax. swing. jfilechooser; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. jpanel; import javax. swing. jscrollpane; import javax. swing. jtextarea; public class S01 implements actionlistener {jframe JF = NULL; jlabel JL = NULL; jtextarea JTA = NULL; jfilechooser jfc = NULL; private object finally; Public S01 () {JF = new jframe ("filechooser example"); Container JCP = JF. get Contentpane (); JTA = new jtextarea (); jscrollpane JSP = new jscrollpane (JTA); JSP. setpreferredsize (new dimension (350,300); jpanel JP = new jpanel (); jbutton jbt1 = new jbutton ("new file "); jbutton jbt2 = new jbutton ("storage file"); jbt1.addactionlistener (this); jbt2.addactionlistener (this); JP. add (jbt1); JP. add (jbt2); JL = new jlabel ("", jlabel. center); jfc = new jfilechooser ("D: \"); JCP. add (JL, borderlayout. north); JC P. add (JSP, borderlayout. center); JCP. add (JP, borderlayout. south); JF. pack (); JF. setvisible (true); JF. addwindowlistener (New windowadapter () {public void windowclosing (invalid wevent e) {system. exit (0) ;}}) ;}public static void main (string [] ARGs) {New S01 () ;}public void actionreceivmed (actionevent e) {file = NULL; int result; fileinputstream fin = NULL; fileoutputstream fout = NULL; If (E. getactioncommand (). Equals ("new file") {jfc. setapprovebuttontext ("OK"); jfc. setdialogtitle ("Open File"); Result = jfc. showopendialog (Jf); JTA. settext (""); If (result = jfc. approve_option) {file = jfc. getselectedfile (); Jl. settext ("the name of the file you selected to open is:" + file. getname ();} else if (result = jfc. cancel_option) {Jl. settext ("You have not selected any file") ;}} if (file! = NULL) {try {fin = new fileinputstream (File);} catch (filenotfoundexception E1) {Jl. settext ("file not found"); Return ;}} int readbyte; try {While (readbyte = fin. read ())! =-1) {JTA. append (string. valueof (char) readbyte);} catch (ioexception E2) {Jl. settext ("file read error");} finally {try {If (Fin! = NULL) {fin. close () ;}} catch (ioexception E3) {}} if (E. getactioncommand (). equals ("storage file") {result = jfc. showsavedialog (Jf); file = NULL; string filename; If (result = jfc. approve_option) {file = jfc. getselectedfile (); Jl. settext ("the name of the file you selected to store is called:" + file. getname ();} else if (result = jfc. cancel_option) {Jl. settext ("You have not selected any file") ;}} if (file! = NULL) {try {fout = new fileoutputstream (File);} catch (filenotfoundexception E4) {Jl. settext ("file not found"); return;} string content = JTA. gettext (); try {fout. write (content. getbytes ();} catch (ioexception E5) {Jl. settext ("file writing error");} finally {If (fout! = NULL) {try {fout. Close ();} catch (ioexception E6 ){}}}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.