473,811 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ 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.fil echooser.*;
import java.io.File;

public class Mp3Filter extends FileFilter {

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

if (file.isDirecto ry())
return true;

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

else return false;
}

public String getExtension(Fi le file) {
String ext = null;
String fileName = file.getName();
int i = fileName.lastIn dexOf(".");

if (i > 0 && i < fileName.length () - 1)
ext = fileName.substr ing(i+1).toLowe rCase();

return ext;
}

public String getDescription( ) {
return ".mp3 Files";
}
}
Apr 16 '07 #1
3 1920
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.fil echooser.*;
import java.io.File;

public class Mp3Filter extends FileFilter {

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

if (file.isDirecto ry())
return true;

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

else return false;
}

public String getExtension(Fi le file) {
String ext = null;
String fileName = file.getName();
int i = fileName.lastIn dexOf(".");

if (i > 0 && i < fileName.length () - 1)
ext = fileName.substr ing(i+1).toLowe rCase();

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
1546
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 Set cat = CreateObject("ADOX.Catalog") cat.Create newDB
1
1940
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 over the below cod quite a few times and just for the life of me can not figure out wha could be wrong maybe a fresh set of eyes could help me. Thanks i advance! (sorry for the sloppy code as well)
8
2394
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" into 'n'(user specified) different files. However the code currently..though makes 'n' different files... the divided contents are stored only in the first of the 'n' files.
2
1313
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 when under 600 Bulk Storage Tanks
2
2697
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, Office 2000, DDE linkage between Access and Word mail merge docs References used in the Access Module: Visual Basic for Applications Microsoft Access 9.0 Object Library
53
3199
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
1677
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
2048
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 things: - There are some logical errors in my code - The code wasn't actually executed
3
1761
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
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.