Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,804,814 Members 51,601 Now Online
XDA Developers Android and Mobile Development Forum

[Pictorial] [Java] Learn how to create custom FileFilter for .APK files.

Tip us?
 
Beatsleigher
Old
(Last edited by Beatsleigher; 28th February 2014 at 11:31 PM.)
#1  
Beatsleigher's Avatar
Senior Member - OP
Thanks Meter 526
Posts: 1,056
Join Date: Oct 2011
Location: Somewhat around here : 52.366848,8.3345

 
DONATE TO ME
Cool [Pictorial] [Java] Learn how to create custom FileFilter for .APK files.

Hey there, guys!

It's been a while since I've posted something, and Universal Android Toolkit is coming along nicely, so while I'm coding it, I decided to create a pictorial, to show you how you can create a FileFilter for Java JFileChoosers, which are used to detect ONLY .APK-files. Of course you can extend this in any way you choose.

So, let's get started!

Prerequisites:
  • An IDE (Preferrably NetBeans IDE)
  • Basic Java knowledge.
  • A project. (And in that project
  • One (or more) JFileChooser.
  • A way of working with the JFileChooser (a JButton or similar)
  • An empty class somewhere.

Once you have all that set up, open up your new (empty) class.
It should look a bit like this:


Now, before we get on to the coding, we want to tell Java, which type of FileFilter we want to use.
So, we're going to import the following line:
Code:
import javax.swing.filechooser.FileFilter;
It should look a bit like this:


Now, don't freak out over the unused import, that's what we're going to fix, now.
Behind the name of your class (the class identifier), type following code:
Code:
extends FileFilter


If you're ising NetBeans, you'll notice the little error-bulb on the side. To make life easier, click on it and implement all the abstract methods.
If your IDE doesn't have this feature, just type the code from the picture below:


Now, you'll notice that the class is missing a few things.
These things are:
  1. a constructor
  2. a description
  3. a list of file extensions.
But fear not! Here they come!
At the top of the class, create two variables.
One should be a String variable, called description (or whatever you like, to be honest.),
and the other should be a String array called extensions (or whatever you like).
Then, create an empty constructor, as this FileFilter will have everything pre-defined.


Ok! Now that we've got that sorted, let's add some real code to the methods.
Ok, this is what we want the method accept(File f) {} to do:
  1. Check if the file is a directory or not.
  2. If it isn't, check if the file has a valid extension and whether it should be shown.
  3. If everything's OK, then show (accept) the file.

And this is what we want the method getDescription() {} to do:
  1. Show the description of the file type to the user.
This code is very simple and not hard to understand.
But I've commented it, just to be sure.

EDIT:
As @AdamOutler mentioned below, you can use this code instead, and, if you've got a slow(ish) computer, this might also speed things up.
Code:
public boolean accept(){
    return file.isDirectory() || file.getAbsolutePath().lower().endsWith(".apk");
}
So, thanks to him!

Now that we've got our FileFilter finished, let's go ahead to our button and do something!

First, make sure you have imported the class we just created.
I created the class in the package eu.m4gkbeatz.androidtoolkit.io, so that's where I have to look for the class.


Now, create an ActionEvent or some other method, so we can test our JFileChooser's FileFilter.


Once you've got all that done, let's enter the code we need, in order to test our JFileChooser.


Now let's test this!

First, debug your project and make sure it runs error-free:
(Note, you will see a WIP-build of Universal Android Toolkit. I was so busy doing the design and other projects, I haven't quite added alot of controls to it, yet!)
https://db.tt/nyaS6buQ

And then click your button (in my case, the gray one with the three dots) and test the JFileChooser.
And voilá!
If you followed the code properly, you should have done everything right!


If you liked this tutorial, don't forget to shoot me a Thanks!
If you need help, contact me via my Facebook page, Twitter or reply here, so everyone can see your issue.

To view the entire file, click here!

EDIT:
I was asked what rig I'm using, because of the OS X-like design, so I thought I may as well write it here, so no confusion occurs.
I am NOT using Windows.
Instead:
Kubuntu 13.10 x64
KDE 4 Completely Oxygen
The window LAF is Oxygen OS X, found in the System Settings.


My popular projects on GitHub
Universal Android Toolkit is now in BETA, JDroidLib is released!


My work (other) work:
"Nothing is infinite, except human stupidity." - Albert Einstein.
"You shouldn't trust everything you see on the internet." - Abraham Lincoln.

If you haven't already, add me to your circles on Google Plus
The Following 8 Users Say Thank You to Beatsleigher For This Useful Post: [ Click to Expand ]
 
AdamOutler
Old
(Last edited by AdamOutler; 22nd February 2014 at 11:35 PM.)
#2  
AdamOutler's Avatar
Recognized Developer
Thanks Meter 9,619
Posts: 5,195
Join Date: Feb 2011
Location: Louisiana

 
DONATE TO ME
It might simplify things a bit and reduce processing time to use this...

Code:
public boolean accept(){
    return file.isDirectory() || file.getAbsolutePath().lower().endsWith(".apk");
}
Note the .lower(), which will cause it to recognize any combo of apk/APK
Flash with Odin on Windows, Linux and Mac. Use JOdin3, Available in a web browser or offline
Check out my developer pages. Add me to your circles on Google Plus.
Wanna see the longest Linux BASH script ever made? click here.
The Following 3 Users Say Thank You to AdamOutler For This Useful Post: [ Click to Expand ]
 
Beatsleigher
Old
#3  
Beatsleigher's Avatar
Senior Member - OP
Thanks Meter 526
Posts: 1,056
Join Date: Oct 2011
Location: Somewhat around here : 52.366848,8.3345

 
DONATE TO ME
Quote:
Originally Posted by AdamOutler View Post
It might simplify things a bit and reduce processing time to use this...

Code:
public boolean accept(){
    return file.isDirectory() || file.getAbsolutePath().lower().endsWith(".apk");
}
Note the .lower(), which will cause it to recognize any combo of apk/APK
Ah, thanks!

Didn't think of that, before. I'll add it to the OP.


My popular projects on GitHub
Universal Android Toolkit is now in BETA, JDroidLib is released!


My work (other) work:
"Nothing is infinite, except human stupidity." - Albert Einstein.
"You shouldn't trust everything you see on the internet." - Abraham Lincoln.

If you haven't already, add me to your circles on Google Plus
The Following User Says Thank You to Beatsleigher For This Useful Post: [ Click to Expand ]
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes