Lwuit TabbedPane无更新

来源:互联网 发布:性价比高的护肤品知乎 编辑:程序博客网 时间:2024/06/13 00:17
写了段代码,要显示文件目录及目录下的文件。要求使用TabbedPane(因为多个Pane显示不同的内容)。现我在实现一个显示本地文件目录的代码如下,可以显示根目录了,但是在点击root/后,Form突然就清空了,没有TabbedPane了。为什么???

谢谢....



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.TabbedPane;
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 Jsr75Test04 extends MIDlet {
private Form f;
private List directoryList;
private TabbedPane tpBrowser;
private String currDirName = "";
public final static String UPStr = "UP..";

public Jsr75Test04() {
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");
tpBrowser = new TabbedPane();
f.setLayout(new BorderLayout());
f.addComponent(BorderLayout.CENTER, tpBrowser);
directoryList = createDirectoryList();
tpBrowser.addTab("ViewEntry", 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();
tpBrowser.removeTabAt(0);
tpBrowser.addTab("ViewEntry", directoryList);
f.revalidate();
} else if (selStr.endsWith("/")) {
f.removeAll();
currDirName += selStr;
directoryList = createDirectoryList();
tpBrowser.removeTabAt(0);
tpBrowser.addTab("ViewEntry", directoryList);
f.revalidate();
}
}
}

0 0
原创粉丝点击