Expand|Select|Wrap|Line Numbers
- dir = new File("C:\\java");
- FileTableModel model = new FileTableModel(dir);
- TableSorter sorter1 = new TableSorter(model);
- table = new JTable(sorter1);
- sorter1.setTableHeader(table.getTableHeader());
- sorter1.setSortingStatus(0, -1);
- TableColumn column = null;
- for (int i = 0; i < 3; i++) {
- column = table.getColumnModel().getColumn(i);
- if (i == 0) {
- column.setPreferredWidth(20);
- } else {
- if (i == 1){
- column.setPreferredWidth(160);
- }
- else
- {
- column.setPreferredWidth(80);
- }
- }
- }
- ListSelectionModel listMod1 = table.getSelectionModel();
- listMod1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
- listMod1.addListSelectionListener(this);
- table.addMouseListener(this);
- tLocal.setText(dir.getPath());
- table.getTableHeader().setReorderingAllowed(false);
- table.setRowHeight(20);
- scrClient = new JScrollPane(table);
The filetable is created with this code:
Expand|Select|Wrap|Line Numbers
- class FileTableModel extends AbstractTableModel {
- protected File dir;
- protected String[] filenames;
- protected String[] columnNames = new String[] {
- "","name", "size", "last modified", "directory?", "readable?", "writable?"
- };
- protected Class[] columnClasses = new Class[] {
- Icon.class,String.class, Long.class, Date.class,
- Boolean.class, Boolean.class, Boolean.class
- };
- // 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;
- }
- }
- }