The Artima Developer Community
Sponsored Link

Java Answers Forum
local FileTableModel

1 reply on 1 page. Most recent reply: Apr 19, 2007 3:19 AM by Thomas Sels

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Thomas Sels

Posts: 4
Nickname: satanduvel
Registered: Apr, 2007

local FileTableModel Posted: Apr 18, 2007 11:50 PM
Reply to this message Reply
Advertisement
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 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;
}
}
}


Thomas Sels

Posts: 4
Nickname: satanduvel
Registered: Apr, 2007

Re: local FileTableModel Posted: Apr 19, 2007 3:19 AM
Reply to this message Reply
I have found the solution here is the code:

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

Flat View: This topic has 1 reply on 1 page
Topic: local FileTableModel Previous Topic   Next Topic Topic: Java programmers required

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use