dcsimg
I am building a basic application and would like to include a menu item which includes a 'recently used file' list using AWT only
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   rob_pretty
Posted On:   Tuesday, April 30, 2002 11:54 AM

I have an application which i would like ot add a 'recently used file' list to the the menu. currentlky i have a submenu included inmy File menu and would like to populate the recently used file list from an arry. i,.e each time a file is used a method public void adtoRecntlist() would
becalled to add the file to the list and refesh the menu item to include this item. So far i can only find exampleof how to initiaise thislist with fixed entries. When i have tried to refresh the list it simply remains as initialesd? do i ned to incorprate a repaint() or readd
menubvar to my app? the app uses a Fram and a standard AWT menubar any clues/examples?
cheers
rob

Re: I am building a basic application and would like to include a menu item which includes a 'recently used file' list using AWT only

Posted By:   Rahi_Parsi  
Posted On:   Thursday, June 13, 2002 10:05 AM

Well since you know how to create/use menus with AWT, the task is a simple matter of storing the previously used files in a list which will be read from a file when the application is launched, and updated when closed.


I would recommend the following:

public class UsedFiles extends Object implements Serializable
{ protected String filenames[]=null;

public UsedFiles(int size){ super(); filenames=new String[size];}

public UsedFiles(String fnames[])
{ super(); filenames=fnames;}

protected int VerifyIndex(int index)
{ int n=index;
if(index<0){ n=0;}
else if(index>filenames.length-1){ n=filenames.length-1;}
return n;
}

public int getFileCount(){ return filenames.length;}

public String getFileName(int index)
{ return filenames[VerifyIndex(index)];}

public void setFileName(String fname,int index)
{ filenames[VerifyIndex(index)]=fname;}

public static UsedFiles readList(InputStream instream) throws IOException
{ UsedFiles files=null;
ObjectInputStream obstream=new ObjectInputStream(instream);
files=(UsedFiles)(obstream.readObject());
objstream.close();
return files;
}

public static void writeList(OutputStream oustream,UsedFiles files) throws IOException
{ ObjectOutputStream obstream=new ObjectOutputStream(oustream);
obstream.writeObject(files);
obstream.flush();
obstream.close();
}
}



You would use the class like this:

UsedFiles files=UsedFiles.readList(new FileInputStream("files.dat"));
files.setFileName("essay.doc",2);
UsedFiles.writeList(new FileOutputStream("files.dat"),files);
About | Sitemap | Contact