File dialog box

Source: Internet
Author: User

JFileChooser class can create file dialog box

There are two ways to save and open the following

Showsavedialog (Component a); Showopendialog (Component a);

Parameter A is the location of the file dialog box, NULL is in the center of the screen, this is in the center of the window

The above method returns two constants when the "OK", "Cancel", "Close" of the User Action dialog box

Jfilechooser.approve_option

Jfilechooser.cancel_option

If you want the file dialog box to manipulate the file type is required, such as "JPG" graphic ah, you can

Filenameextensionfilter filter1=new filenameextensionfilter ("Graphics file", "JPG", "gif")

The Setfilefilter (Filenameextensionfilter filter) method is then invoked with an instance of the file dialog box

Test code

 PackageTestwin;ImportJava.awt.EventQueue;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.io.*;Importjavax.swing.*;ImportJavax.swing.JFrame;ImportJavax.swing.JScrollPane;Importjava.awt.BorderLayout;ImportJavax.swing.JTextField;ImportJavax.swing.filechooser.FileNameExtensionFilter; Public classTestwin { Public Static voidMain (String args[]) {Mywin win1=NewMywin (); }}classMywinextendsJFrameImplementsactionlistener{jfilechooser file1;//file dialog BoxJMenuBar menubar;    JMenu menu;    JMenuItem Save,open;    JTextArea text; BufferedReader in1;//Buffered InputFileReader FileReader;//file character inputBufferedWriter out1;//Buffered OutputFileWriter FileWriter;//file character OutputMywin () {init (); SetBounds (30,30,300,400); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }    voidinit () {text=NewJTextArea (10,10); Add (NewJScrollPane (text), borderlayout.center); MenuBar=NewJMenuBar (); Menu=NewJMenu ("File"); Save=NewJMenuItem ("Save"); Open=NewJMenuItem ("Open"); Save.addactionlistener ( This); Open.addactionlistener ( This);        Menu.add (save);        Menu.add (open);        Menubar.add (menu);        Setjmenubar (menubar); File1=NewJFileChooser (); Filenameextensionfilter Filter=NewFilenameextensionfilter ("Java file", "TXT");//file name FilteringFile1.setfilefilter (filter);//Filter Down    }     Public voidactionperformed (ActionEvent e) {if(E.getsource () = =Save) {            intState=file1.showsavedialog ( This);//Display the Save box            if(state==jfilechooser.approve_option) {                Try{File dir=file1.getcurrentdirectory ();//Get save PathString name=file1.getselectedfile (). GetName ();//get the name of the choiceFile file=NewFile (dir,name); FileWriter=NewFileWriter (file); OUT1=NewBufferedWriter (FileWriter);                    Out1.write (Text.gettext ());                    Out1.close ();                Filewriter.close (); }                Catch(IOException exp) {}}} Else if(E.getsource () = =Open) {            intState=file1.showopendialog ( This); if(state==jfilechooser.approve_option) {                Try{File dir=file1.getcurrentdirectory (); String name=file1.getselectedfile (). GetName (); File File=NewFile (dir,name); FileReader=Newfilereader (file); In1=NewBufferedReader (FileReader); String s=NULL;  for(;(s=in1.readline ())! =NULL;) {text.append (s+ "\ n");                    } in1.close ();                Filereader.close (); }                Catch(IOException exp) {}}} }}

File dialog box

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.