Home > JavaFX > File Chooser with Filter

File Chooser with Filter

10 February 2010 Leave a comment Go to comments

ScreenShot

File Chooser pada JavaFX di-import dari JFileChooser. Pada kasus ini, file chooser akan memfilter file-file tertentu saja (.jpg). Kemudian menampilkan langsung pada ImageView.

Karena Class FileFilter adalah class abstrac, maka kita harus override function “accept” dan “getDescription”.

//...others code
fileFilter = FileFilter{
      override public function accept (file : File) : Boolean {
            if(file.getName().toLowerCase().endsWith(".jpg") or file.isDirectory()){
                return true;
            }
            return false;
       }
       override public function getDescription () : String {
           return ".jpg";
       }
}
//...other code

File Chooser akan tampil pada saat button “Browse” diklik.

//...others code
button = Button{
      text: "Browse"
      translateX: 10
      translateY:10;
      action: function() {
             fileChooser.addChoosableFileFilter(fileFilter);
             if ( fileChooser.showDialog(null, "Select")
                 == JFileChooser.APPROVE_OPTION){
                    fileName = fileChooser.getSelectedFile().getName();
                    file = fileChooser.getSelectedFile().toURL().toExternalForm();
                    imageView.image = Image{
                         url:file
                    }
            }
      }
}
//...others code

Download Source Code lengkap

Thanks…. Terus berkreasi dan lebih creatif….!!!

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: