Results 1 to 8 of 8
  1. #1
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default 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;
            }
    I got this message

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0

    what I am supposed to do ?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default 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.

  3. #3
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default 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;
            }
    I think the problem is the filter in the getChild and getChildCount
    "String[] children = directory.list(new filterJava());"
    am I ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default 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)
    shows error happened at line 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.

  5. #5
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default 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 ?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default 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.

  7. #7
    Fadly Massere is offline Member
    Join Date
    Nov 2013
    Location
    Jakarta
    Posts
    12
    Rep Power
    0

    Default Re: File Filter in JTree

    Quote Originally Posted by Norm View Post
    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 ???
    can u give ne an example ?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default 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

  1. Pipe-and-Filter Architecture for txt file.
    By saziz94 in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-26-2012, 06:23 PM
  2. File Extension Filter
    By heartysnowy in forum New To Java
    Replies: 9
    Last Post: 10-09-2010, 02:33 PM
  3. Mbox File and JTree Problem.
    By dracuswolf in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2009, 06:17 AM
  4. Hi please help with a file filter!
    By xbox_nutter in forum New To Java
    Replies: 4
    Last Post: 03-23-2009, 01:09 PM
  5. How to filter files in file upload using html contorl
    By deivaganesh in forum Advanced Java
    Replies: 0
    Last Post: 01-29-2008, 07:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •