[Android Software Development] Beatsleigher:
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:

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:

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:

a constructor

a description

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:

Check if the file is a directory or not.

If it isn't, check if the file has a valid extension and whether it should be shown.

If everything's OK, then show (accept) the file.

And this is what we want the method getDescription() {} to do:

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 [MENTION=3682533]AdamOutler[/MENTION] mentioned below, you can use this code instead, and, if you've got a slow(ish) computer, this might also speed things up.

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.