473,473 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

plz help me with this code (a FileFilter for JFileChooser)

2 New Member
Hi!

This code wont work properly, it returns true for showing all maps, but not any files. It is supposed to filter the JFileChooser to only show *.mp3 files.
I have looked at it too long now, so i think its best to let someone else to look at it now :p

Here it is:

import javax.swing.filechooser.*;
import java.io.File;

public class Mp3Filter extends FileFilter {

public boolean accept(File file) {
String ext = getExtension(file);

if (file.isDirectory())
return true;

else if (ext != null && ext == "mp3")
return true;

else return false;
}

public String getExtension(File file) {
String ext = null;
String fileName = file.getName();
int i = fileName.lastIndexOf(".");

if (i > 0 && i < fileName.length() - 1)
ext = fileName.substring(i+1).toLowerCase();

return ext;
}

public String getDescription() {
return ".mp3 Files";
}
}
Apr 16 '07 #1
3 1892
r035198x
13,262 MVP
Hi!

This code wont work properly, it returns true for showing all maps, but not any files. It is supposed to filter the JFileChooser to only show *.mp3 files.
I have looked at it too long now, so i think its best to let someone else to look at it now :p

Here it is:

import javax.swing.filechooser.*;
import java.io.File;

public class Mp3Filter extends FileFilter {

public boolean accept(File file) {
String ext = getExtension(file);

if (file.isDirectory())
return true;

else if (ext != null && ext == "mp3")
return true;

else return false;
}

public String getExtension(File file) {
String ext = null;
String fileName = file.getName();
int i = fileName.lastIndexOf(".");

if (i > 0 && i < fileName.length() - 1)
ext = fileName.substring(i+1).toLowerCase();

return ext;
}

public String getDescription() {
return ".mp3 Files";
}
}
1.) Remember to wrap code in code tags next time you post code.
2.) When comparing Strings in java, do not use ==. Use the .equals method. e.g
Expand|Select|Wrap|Line Numbers
  1. ext.equals("mp3")
Apr 17 '07 #2
JosAH
11,448 Recognized Expert MVP
The String class has a very convenient method for this: endsWith().

kind regards,

Jos
Apr 17 '07 #3
FinPingvin
2 New Member
Sorry r035198x, my first post on the forums.
Ah and you are so right, guess i was kind of tired last night hehe.

and thanks josAH, endsWith() was a handy method!
Apr 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: michaaal | last post by:
I am trying to run this code on a Windows 2003 Server. I have ASP enabled. Any idea why it doesn't work? <% newDB = "new.mdb" newDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & newDB...
1
by: Kewlb | last post by:
Hey Guys, My server decided to all of a sudden stop giving me compile/scrip errors and instead now just gives me a blank page with no informatio as to what the problem might be. I have looked...
8
by: sachin bond | last post by:
The following code(in c++) is supposed to divide a file into 'n' different files. suppose i'm having a file called "input.zip". The execution of the following code should divide "input.zip"...
2
by: kmunderwood | last post by:
How do I show only one Element with this code Help Me, OH!!! Help Me ;) I have code that works, but... This is the way it comes out in html The Level, or Element "v1"(background) turns red...
2
by: blongmire | last post by:
.... I know just enough to be dangerous, but the real danger is that I might fall asleep and hit my head on my keyboard waiting for this code to finish executing. Some preliminaries: WinXP Pro,...
53
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
21
by: onkar | last post by:
#include<stdio.h> int i; int i; int main(){ printf("i=%d\n",i); return 0; } Note : I am using gcc-3.4.3 on i386-redhat-linux
1
by: Jeff | last post by:
hi ..Net 2.0 I've created the code below and think I've executed it. After I thought I've executed it I checked in EventLog on the computer (win2k3) and no entry was added. This could mean 2...
3
by: Ramesh Jos | last post by:
Hi, While i was going through this code, I couldn't understand what these pieces of code are going to do ... Can anyone help me understand it ?? #!/usr/bin/perl package Person;
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.