local FileTableModel

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);

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:

class FileTableModelextends 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

publicint getColumnCount(){return 3;}// A constant for this model

publicint 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){

returnnull;

}else{

returnnew 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:returnnull;

}

}

}

[6295 byte] By [Satanduvela] at [2007-11-27 1:35:02]
# 1

Maybe something like this:

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+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;

}

}

}

}

Rodney_McKaya at 2007-7-12 0:43:02 > top of Java-index,Desktop,Core GUI APIs...
# 2
thx mate you're stars wil be givenSatanduvel
Satanduvela at 2007-7-12 0:43:02 > top of Java-index,Desktop,Core GUI APIs...