|
hey...
I currently have a Jtable, allows only 1 row selection at a time, it is used for listing the files in a certain directory..
//////////////////
//This will get all the files from the certain directory to populate the JTable,
// What I need to do now is extentison type filter, sort by the type and the size, not include any sub-directory ONLY files
public Object[][] getFileStats(File dir) {
String files[] = dir.list();
Object[][] results = new Object[files.length][titles.length];
int j=0; // this is to remove folders from the table
for (int i = 0; i < files.length; i++) {
File tmp = new File(files);
boolean isDir = tmp.isDirectory();
if (isDir) {
// dir is a directory
j++;
} else {
// dir is a file
// NEED TO FILTER BY THE FILE EXTENTION, FOR EXAMPLE ONLY *.txt,*.jpg
// ????
results[i-j][0] = tmp.getName();
results[i-j][1] = new Long(tmp.length());
results[i-j][2] = new Date(tmp.lastModified());
// THE BOTTOM ROWS ARE ALL BLANK NOW, BECAUSE OF THE FOLDERS, HOW DO I REMOVE THEM FROM THE "results" ARRAY ??
}
}
return results;
}
//////////////////
// create JTable and add the content into it
// .........
table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (ALLOW_ROW_SELECTION) { // true by default
ListSelectionModel rowSM = table2.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) return;
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("No rows are selected.");
} else {
selectedRow = lsm.getMinSelectionIndex();
System.out.println("Row " + selectedRow + " is now selected.");
javax.swing.table.TableModel model = table2.getModel();
System.out.print(" " + model.getValueAt(selectedRow, 0));
// NEED TO VERIFY IF FILE EXISTS
File f10 = new File((String)model.getValueAt(selectedRow, 0));
System.out.println(f10.exists() ? "exists" : "does not exist");
// IF NOT EXIST, REMOVE ROW
// if (f10.exists()){ System.out.println(" "); } else {model.removeRow(selectedRow); } //// ERROR ?????
}
}
});
}
//////////////////
Also I wanted to know if there is any way to sync. the table in real-time,
for example, if a new file is created, then it will automatically be added to the table, or a file is renamed, so the old file will not exist so the row is deleted but a row row is created with the new filename ??
any ideas?
|
|
|
} else {
// To modify the model it must be a DefaultTableModel or
// one you build yourself. The TableModel interface does
// not have a removeRow method.
model.removeRow(selectedRow); //// ERROR ?????
}
|
|
Also I wanted to know if there is any way to sync. the table in real-time,
for example, if a new file is created, then it will automatically be added to the table, or a file is renamed, so the old file will not exist so the row is deleted but a row row is created with the new filename ??
Yes. These changes will take place in an event listener of some kind or you need to make arrangements to listen for the changes that are to be made for/to your table data. In the code for or called by this listener:
1 - get a reference to the DefaultTableModel for the table
2 - add, change the column values of or remove the desired row
3 - using the addRow or removeRow methods will generate the notices to listeners needed to update the model. If the data was changed tell the model with fireTableCellUpdate, an AbstractTableModel (super)class method. The JTable listens to its model which tells the table to update itself so you only need to make changes to the model.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class FileTable implements ListSelectionListener {
JTable table;
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int row = table.getSelectedRow();
DefaultTableModel model = (DefaultTableModel)table.getModel();
System.out.println("valueAt(" + row + ", 0) = " +
model.getValueAt(row, 0));
}
}
private JScrollPane getContent() {
File dir = new File(".");
dir.setReadOnly();
Object[][] data = getFileData(dir);
Object[] colIds = { "Name", "Size", "Last Modified" };
DefaultTableModel model = new DefaultTableModel(data, colIds);
table = new JTable(model) {
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table.setDefaultRenderer(Date.class, new DateRenderer());
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(this);
return new JScrollPane(table);
}
private Object[][] getFileData(File dir) {
File[] files = dir.listFiles(javaFilter);
int fileCount = getFileCount(files);
int columns = 3;
Object[][] results = new Object[fileCount][columns];
for (int j = 0; j < files.length; j++) {
results[j][0] = files[j].getName();
results[j][1] = new Long(files[j].length());
results[j][2] = new Date(files[j].lastModified());
}
return results;
}
private int getFileCount(File[] files) {
int count = 0;
for(int j = 0; j < files.length; j++) {
if(files[j].isFile())
count++;
}
System.out.println("count = " + count);
return count;
}
private String getExtension(File file) {
String s = file.getPath();
int dot = s.lastIndexOf(".");
return s.substring(dot+1);
}
private FileFilter javaFilter = new FileFilter() {
String JAVA = "java";
public boolean accept(File file) {
return getExtension(file).equalsIgnoreCase(JAVA);
}
};
private class DateRenderer extends DefaultTableCellRenderer.UIResource {
DateFormat formatter = new SimpleDateFormat("HHmm ddMMMyyyy");
public DateRenderer() { super(); }
public void setValue(Object value) {
setText((value == null) ? "" : formatter.format(value));
}
}
public static void main(String[] args) {
FileTable test = new FileTable();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getContent());
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}
|
|
|
|
|
crwood, thanks alot.. i got the file extention part done..
i'll work on the sort of the files later..should not be difficult..
now for the sync. part.. i'm trying to google a few example on how to do this, I understand the concept, but the writing the code is a different.. if you have any link to an example, let me know..
|
|
|
hmm.. i thought it over, and made a function called refresh() which basically, checks the directory and gets the files with the correct extention into an array..
now what i do is have a button called "Refresh" and onclick it runs thats function above, and reloads the table, works nicely but there is no sync. it.. because i am still finding an example to teach me to use threads..
jus need help on finding the example...
then i guess i'll jus use a timer and call that function to sync. the table ...
|
|
|
|
|
|
|
// |