[JAVA100 Example]017, file dialog box

Source: Internet
Author: User
Tags filter save file

Import Java.io.File;


import javax.swing.*;


import javax.swing.filechooser.*;


/**


* <p>title: File filter Demo </p>


* <p>description:filechooserdemo file filter </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:MyFilter.java</p>


* @version 1.0


*/





public class Myfilter extends FileFilter {


private String files;


public Boolean accept (File f) {


if (f.isdirectory ()) {


return true;


    }





String Extension = getextension (f);


if (extension!= null) {


     


if (extension.equals ("Java")) {//Define filtering Java file


return true;


} else {


return false;


      }





}





return false;


  }





//Filter Description


public String getdescription () {


return "Java";


  }


/**


*<br> Method Description: Get the file name extension


*<br> input Parameters:


*<br> return type:


*/


public static String getextension (File f) {


String ext = null;


String s = f.getname ();


int i = S.lastindexof (´.´);





if (i > 0 && i < s.length ()-1) {


ext = s.substring (i+1). toLowerCase ();


    }


return ext;


  }


}





import java.io.*;


import java.awt.*;


import java.awt.event.*;


import javax.swing.*;


Import javax.swing.filechooser.*;


/**


* <p>title: File Dialog Demo </p>


* <p>description: Demo Open File dialog box and save File dialog box, use file filter. </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:FileChooserDemo.java</p>


* @version 1.0


*/





public class Filechooserdemo extends JPanel


implements ActionListener {


static private final String newline = "\ n";


JButton Openbutton, Savebutton;


JTextArea log;


JFileChooser FC;





public Filechooserdemo () {


Super (New BorderLayout ());





log = new JTextArea (15,40);


Log.setmargin (New Insets (10,10,10,10));


log.seteditable (FALSE);


JScrollPane Logscrollpane = new JScrollPane (log);





//Create a file filter using the current directory


FC = new JFileChooser (".");


//filter conditions are defined in the Myfilter class


Fc.addchoosablefilefilter (New Myfilter ());





Openbutton = new JButton ("Open File",


Createimageicon ("Images/open16.gif"));


Openbutton.addactionlistener (this);





Savebutton = new JButton ("Save File",


Createimageicon ("Images/save16.gif"));


Savebutton.addactionlistener (this);





//Build a panel, add "Open File" and "Save File"


JPanel Buttonpanel = new JPanel ();


Buttonpanel.add (Openbutton);


Buttonpanel.add (Savebutton);





Add (Buttonpanel, Borderlayout.page_start);


Add (Logscrollpane, borderlayout.center);


  }


/**


*<br> Method Description: Event handling


*<br> input Parameters:


*<br> return type:


*/


public void actionperformed (ActionEvent e) {





//When clicking on the "Open File" button


if (e.getsource () = = Openbutton) {


int returnval = Fc.showopendialog (filechooserdemo.this);





if (returnval = = jfilechooser.approve_option) {


File File = Fc.getselectedfile ();


//Add some processing to the file here


log.append ("Open file:" + file.getname () + newline);


} else {


log.append ("Open file cancelled by user!") "+ newline);


      }





//Click on the "Save File" button


} else if (E.getsource () = Savebutton) {


int returnval = Fc.showsavedialog (filechooserdemo.this);


if (returnval = = jfilechooser.approve_option) {


File File = Fc.getselectedfile ();


//Add some processing to the file here


log.append ("Save file:" + file.getname () + newline);


} else {


log.append ("Save file canceled by user!") "+ newline);


      }


    }


  }


/**


*<br> Method Description: Get Image Object


*<br> input parameter: String path picture Path


*<br> return type: ImageIcon Picture Object


*/


protected static ImageIcon Createimageicon (String path) {


Java.net.URL imgurl = FileChooserDemo.class.getResource (path);


if (imgurl!= null) {


return new ImageIcon (Imgurl);


} else {


System.err.println ("couldn´t Find File:" + path);


return null;


    }


  }





public static void Main (string[] args) {


Jframe.setdefaultlookandfeeldecorated (TRUE);


jdialog.setdefaultlookandfeeldecorated (TRUE);





//Create form


JFrame frame = new JFrame ("Filechooserdemo");


frame.setdefaultcloseoperation (jframe.exit_on_close);





//Create a panel


jcomponent Newcontentpane = new Filechooserdemo ();


Newcontentpane.setopaque (TRUE);


Frame.setcontentpane (Newcontentpane);





//Display form


Frame.pack ();


frame.setvisible (TRUE);


  }


}

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.