简单的一个jsr75例子--浏览本机文件夹及文件
2009-11-18 18:17
387 查看
效果图:
code:
code:
package lab.sodino.jsr75; import java.io.IOException; import java.util.Enumeration; import java.util.Vector; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; import javax.microedition.io.file.FileSystemRegistry; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import com.sun.lwuit.Display; import com.sun.lwuit.Form; import com.sun.lwuit.List; import com.sun.lwuit.events.ActionEvent; import com.sun.lwuit.events.ActionListener; import com.sun.lwuit.layouts.BorderLayout; import com.sun.lwuit.list.DefaultListModel; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; public class Jsr75Test03 extends MIDlet { private Form f; private List directoryList; private String currDirName = ""; public final static String UPStr = "UP.."; public Jsr75Test03() { Display.init(this); try { Resources r = Resources.open("/javaTheme.res"); UIManager.getInstance().setThemeProps(r.getTheme("javaTheme")); } catch (IOException e) { e.printStackTrace(); } f = new Form("JSR75Form"); f.setLayout(new BorderLayout()); directoryList = createDirectoryList(); f.addComponent(BorderLayout.CENTER, directoryList); } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { // TODO Auto-generated method stub } protected void pauseApp() { // TODO Auto-generated method stub } protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub f.show(); } private Vector getTraverseEntryItem(String currDirName) { Enumeration e = null; Vector vecItems = new Vector(); if (currDirName.equals("")) { e = FileSystemRegistry.listRoots(); } else { try { FileConnection currDir = (FileConnection) Connector.open("file:///" + currDirName); e = currDir.list(); currDir.close(); } catch (IOException e1) { System.out.println("go to catch file:///" + currDirName); e1.printStackTrace(); } } //Folders first and then files, alphabetically arranged int folderCount = 0; while (e.hasMoreElements()) { String fileName = (String) (e.nextElement()); if (fileName.endsWith("/")) { System.out.println("folder added " + fileName + " folderCount = " + folderCount); vecItems.insertElementAt(fileName, folderCount); folderCount++; } else { System.out.println("file added " + fileName); vecItems.addElement(fileName); } } //if view a sub-directory folder, added the UPStr at the head. if (currDirName.equals("") == false) { vecItems.insertElementAt(UPStr, 0); } return vecItems; } private List createDirectoryList() { DefaultListModel model = new DefaultListModel(getTraverseEntryItem(currDirName)); final List list = new List(model); list.setFixedSelection(List.FIXED_NONE_CYCLIC); list.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { updateCurrDirName(list.getSelectedItem().toString()); System.out.println("sodino : " + list.getSelectedItem().toString()); } }); return list; } private void updateCurrDirName(String selStr) { if (selStr.equals(UPStr)) { f.removeAll(); int i = currDirName.lastIndexOf('/', currDirName.length() - 2); if (i == -1) { currDirName = ""; } else { currDirName = currDirName.substring(0, i + 1); } directoryList = createDirectoryList(); f.addComponent(BorderLayout.CENTER, directoryList); f.revalidate(); } else if (selStr.endsWith("/")) { f.removeAll(); currDirName += selStr; directoryList = createDirectoryList(); f.addComponent(BorderLayout.CENTER, directoryList); f.revalidate(); } } }
相关文章推荐
- 一个简单打开D盘所有文件夹和文件的例子以及动态创建双击事件
- web.config文件自定义配置节的使用方法的一个简单例子
- Flash Air2 一个保存本地文件的简单例子。
- Windows下,当一个文件夹里包含几十万个1K左右的小文件时(举个例子),最有效的删除方法。
- 将本地CSV格式文件内容上传到服务器的一个简单例子
- linux 下文件操作和内存映射的一个简单例子
- 一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- 转:一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- 一个简单的利用ini配置文件以及用户输入的交互进行文件夹内容拷贝的批处理文件脚本的实现
- PHP循环输出指定目录下的所有文件和文件夹路径例子(简单实用)
- 【转载】关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 高级操作
- java.nio将一个文件的内容写入到另一个的文件简单例子
- 一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- C++——一个简单的多文件具体例子
- 【自用】C# 遍历指定文件夹下的文件的一个简单方法
- 安卓--一个简单的SD卡文件浏览程序
- 收藏——在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- python (1)一个简单的爬虫: python 在windows下 创建文件夹并写入文件
- 一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- web.config文件自定义配置节的使用方法的一个简单例子