Hey i have a "problem" with mine FileTableModel. I get al the values in my JTable but there isn't a ".." directory in the JTable. So i want to create a row with the option to go 1 directory up. How do i do that?
dir = new File("C:\\java"); FileTableModel model = new FileTableModel(dir);
TableSorter sorter1 = new TableSorter(model); table = new JTable(sorter1);
// This table model works for any one given directory public FileTableModel(File dir) { this.dir = dir; this.filenames = dir.list(); // Store a list of files in the directory }
// These are easy methods public int getColumnCount() { return 3; } // A constant for this model public int getRowCount() { return filenames.length; } // # of files in dir
// Information about each column public String getColumnName(int col) { return columnNames[col]; } public Class getColumnClass(int col) { return columnClasses[col]; }
// The method that must actually return the value of each cell public Object getValueAt(int row, int col) { File f = new File(dir, filenames[row]); switch(col) { case 0: return f.isDirectory() ? folder : files; case 1: return filenames[row]; case 2: if (f.isDirectory()==true){ return null; }else{ return new Long(f.length()); } case 3: return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE; case 4: return f.canRead() ? Boolean.TRUE : Boolean.FALSE; case 5: return f.canWrite() ? Boolean.TRUE : Boolean.FALSE; default: return null; } } }
// This table model works for any one given directory public FileTableModel(File dir) { this.dir = dir; this.filenames = dir.list(); // Store a list of files in the directory }
// These are easy methods public int getColumnCount() { return 3; } // A constant for this model public int getRowCount() { return filenames.length+1; } // # of files in dir
// Information about each column public String getColumnName(int col) { return columnNames[col]; } public Class getColumnClass(int col) { return columnClasses[col]; }
// The method that must actually return the value of each cell public Object getValueAt(int row, int col) { if (row == 0) { switch(col) { case 0: return folder; case 1: return ".."; case 2: return null; case 3: return Boolean.TRUE; case 4: return Boolean.FALSE; case 5: return Boolean.FALSE; default: return null; } } else { File f = new File(dir, filenames[row-1]); switch(col) { case 0: return f.isDirectory() ? folder : files; case 1: return filenames[row-1]; case 2: if (f.isDirectory()==true){ return null; }else{ return new Long(f.length()); } case 3: return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE; case 4: return f.canRead() ? Boolean.TRUE : Boolean.FALSE; case 5: return f.canWrite() ? Boolean.TRUE : Boolean.FALSE; default: return null; } } } }