Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

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

OP Beatsleigher

19th February 2014, 03:17 PM   |  #1  
Beatsleigher's Avatar
OP Senior Member
Flag Somewhat around here : 52.366848,8.3345
Thanks Meter: 554
 
1,076 posts
Join Date:Joined: Oct 2011
Donate to Me
More
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.
Last edited by Beatsleigher; 1st March 2014 at 12:31 AM.
The Following 8 Users Say Thank You to Beatsleigher For This Useful Post: [ View ]
23rd February 2014, 12:33 AM   |  #2  
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
Last edited by AdamOutler; 23rd February 2014 at 12:35 AM.
The Following 3 Users Say Thank You to AdamOutler For This Useful Post: [ View ]
28th February 2014, 02:59 AM   |  #3  
Beatsleigher's Avatar
OP Senior Member
Flag Somewhat around here : 52.366848,8.3345
Thanks Meter: 554
 
1,076 posts
Join Date:Joined: Oct 2011
Donate to Me
More
Quote:
Originally Posted by AdamOutler

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.
The Following User Says Thank You to Beatsleigher For This Useful Post: [ View ]
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes