473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. dir = new File("C:\\java");
  2.             FileTableModel model = new FileTableModel(dir);
  3.  
  4.             TableSorter sorter1 = new TableSorter(model);
  5.             table = new JTable(sorter1);
  6.  
  7.             sorter1.setTableHeader(table.getTableHeader());
  8.             sorter1.setSortingStatus(0, -1);
  9.  
  10.             TableColumn column = null;
  11.             for (int i = 0; i < 3; i++) {
  12.                 column = table.getColumnModel().getColumn(i);
  13.                 if (i == 0) {
  14.                     column.setPreferredWidth(20);
  15.                 } else {
  16.                     if (i == 1){
  17.                         column.setPreferredWidth(160);
  18.                     }
  19.                     else
  20.                     {
  21.                         column.setPreferredWidth(80);
  22.                     }
  23.                 }
  24.             }
  25.             ListSelectionModel listMod1 =  table.getSelectionModel();
  26.             listMod1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  27.             listMod1.addListSelectionListener(this);
  28.  
  29.             table.addMouseListener(this);
  30.             tLocal.setText(dir.getPath());
  31.             table.getTableHeader().setReorderingAllowed(false);
  32.             table.setRowHeight(20);
  33.  
  34.             scrClient = new JScrollPane(table);


The filetable is created with this code:

Expand|Select|Wrap|Line Numbers
  1. class FileTableModel extends AbstractTableModel {
  2.           protected File dir;
  3.           protected String[] filenames;
  4.  
  5.           protected String[] columnNames = new String[] {
  6.             "","name", "size", "last modified", "directory?", "readable?", "writable?"
  7.           };
  8.  
  9.           protected Class[] columnClasses = new Class[] { 
  10.             Icon.class,String.class, Long.class, Date.class, 
  11.               Boolean.class, Boolean.class, Boolean.class
  12.           };
  13.  
  14.           // This table model works for any one given directory
  15.           public FileTableModel(File dir) { 
  16.             this.dir = dir; 
  17.             this.filenames = dir.list();  // Store a list of files in the directory
  18.           }
  19.  
  20.           // These are easy methods
  21.           public int getColumnCount() { return 3; }  // A constant for this model
  22.           public int getRowCount() { return filenames.length; }  // # of files in dir
  23.  
  24.           // Information about each column
  25.           public String getColumnName(int col) { return columnNames[col]; }
  26.           public Class getColumnClass(int col) { return columnClasses[col]; }
  27.  
  28.           // The method that must actually return the value of each cell
  29.           public Object getValueAt(int row, int col) {
  30.             File f = new File(dir, filenames[row]);
  31.             switch(col) {
  32.             case 0: return f.isDirectory() ? folder : files;
  33.             case 1: return filenames[row];
  34.             case 2: if (f.isDirectory()==true){
  35.                             return null;
  36.                         }else{
  37.                             return new Long(f.length());
  38.                         }
  39.             case 3: return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
  40.             case 4: return f.canRead() ? Boolean.TRUE : Boolean.FALSE;
  41.             case 5: return f.canWrite() ? Boolean.TRUE : Boolean.FALSE;
  42.             default: return null;
  43.             }
  44.           }
  45.          }
Apr 19 '07 #1
0 867

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Steffen | last post by:
Hi! I'm trying to access a EntityBean from a servlet via the bean's local home interface. The EJB and the Servlet are together in one .ear file and I'm using JBoss 3.2.3. I think the...
1
by: jiing | last post by:
Now let me describe what I have done and my purpose: Originally, I want to user ports to install phpBB But I found that phpBB doesn't support mysql 5.x (but the ports installed mySQL 5.0.0...
0
by: Jan | last post by:
While building PHP 4.4.0 on a fresh installed FreeBSD 5.4 I get the following error: www# /bin/sh /usr/home/c/console/toolscon/web/2/php-4.4.0/libtool --silent --preserve-dup-deps...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
6
by: Brad | last post by:
I have a win2003 server workstation with multiple webs, each web has it's own ip address. In VS2005, if I select to open an existing web site, select Local IIS, the dialog correctly displays a...
1
by: orp | last post by:
We've been struggling on how to determine if a local user is already in a local group. We have code (C#) that creates a local user, if the user doesn't already exist. And, we have code (C#) that...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.