Results 1 to 8 of 8
Thread: File Filter in JTree
- 04-04-2014, 07:44 PM #1
Member
- Join Date
- Nov 2013
- Location
- Jakarta
- Posts
- 12
- Rep Power
- 0
File Filter in JTree
Hello,
This is my code,
My goal is to display file that have extension *.java,
What I am suppose to do ?
I was wrote this swing in netbeans,
Java Code:private static class FileSystemModel implements TreeModel { private File root; private Vector listeners = new Vector(); public FileSystemModel(File rootDirectory) { root = rootDirectory; } @Override public Object getRoot() { return root; } class filterJava implements FilenameFilter { @Override public boolean accept(File dir, String name) { String lowerCaseName = name.toLowerCase(); return lowerCaseName.endsWith(".java"); } } @Override public Object getChild(Object parent, int index) { File directory = (File) parent; String[] children = directory.list(new filterJava()); for(int a=0; a < children.length; a++){ System.out.println(children[a]); } return new TreeFile(directory, children[index]); } @Override public int getChildCount(Object parent) { File file = (File) parent; if (file.isDirectory()) { String[] fileList = file.list(new filterJava()); if (fileList != null) { return file.list().length; } } return 0; }
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
what I am supposed to do ?
- 04-04-2014, 08:05 PM #2
Re: File Filter in JTree
What line in the source did that error happen on?
If you don't understand my response, don't ignore it, ask a question.
- 04-04-2014, 09:23 PM #3
Member
- Join Date
- Nov 2013
- Location
- Jakarta
- Posts
- 12
- Rep Power
- 0
Re: File Filter in JTree
there is no error in compile,
when I am execute, the out message just "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0"
The original code is
Java Code:public Object getChild(Object parent, int index) { File directory = (File) parent; String[] children = directory.list(new filterJava()); for(int a=0; a < children.length; a++){ System.out.println(children[a]); } return new TreeFile(directory, children[index]); } @Override public int getChildCount(Object parent) { File file = (File) parent; if (file.isDirectory()) { String[] fileList = file.list(new filterJava()); if (fileList != null) { return file.list().length; } } return 0; }
"String[] children = directory.list(new filterJava());"
am I ?
- 04-04-2014, 09:32 PM #4
Re: File Filter in JTree
The execution time error should show the source line number where the error happened.
For example:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at TestCode17.main(TestCode17.java:241)Last edited by Norm; 04-04-2014 at 09:35 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-04-2014, 10:14 PM #5
Member
- Join Date
- Nov 2013
- Location
- Jakarta
- Posts
- 12
- Rep Power
- 0
Re: File Filter in JTree
this error :
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at Tampilan.Main_Menu$FileSystemModel.getChild(Main_M enu.java:222)
which is
return new TreeFile(directory, children[index]);
now what ?
- 04-04-2014, 10:24 PM #6
Re: File Filter in JTree
The error message says that the array at line 222 has no element at index 0 which means the array is empty.
The code needs to test for that the index into the array does not go past the last element in the array.
Use an if to test that the array has enough elements and
either don't call the constructor
or pass another value to the constructor
or ???If you don't understand my response, don't ignore it, ask a question.
- 04-05-2014, 11:08 AM #7
Member
- Join Date
- Nov 2013
- Location
- Jakarta
- Posts
- 12
- Rep Power
- 0
- 04-05-2014, 02:41 PM #8
Re: File Filter in JTree
I don't know what the getChild() method is supposed to return.
One option might be a null if there are no children:Java Code:if(children.length == 0) // test if there are any children return null; // return null if there are none
If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Pipe-and-Filter Architecture for txt file.
By saziz94 in forum Threads and SynchronizationReplies: 1Last Post: 09-26-2012, 06:23 PM -
File Extension Filter
By heartysnowy in forum New To JavaReplies: 9Last Post: 10-09-2010, 02:33 PM -
Mbox File and JTree Problem.
By dracuswolf in forum AWT / SwingReplies: 1Last Post: 07-26-2009, 06:17 AM -
Hi please help with a file filter!
By xbox_nutter in forum New To JavaReplies: 4Last Post: 03-23-2009, 01:09 PM -
How to filter files in file upload using html contorl
By deivaganesh in forum Advanced JavaReplies: 0Last Post: 01-29-2008, 07:31 AM
Bookmarks