Skip to main content

How to display List from other class in J2me?

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
keihwang
Offline
Joined: 2011-03-20

i have a class for browsing the file from phone memory, it use list to append the file list and i have another class as the mainInterface.
so how i can call that list class into my mainInterface?

here is the file browser class

public class FileBrowser implements CommandListener {

private String currDirName;
protected String fileName;
private Command open = new Command("Open", Command.ITEM, 1);
private Command back = new Command("Back", Command.BACK, 2);
private Command exit = new Command("Exit", Command.EXIT, 3);

private final static String UP_DIR = "...";
private final static String MEGA_ROOT = "/";
private final static String SEP_STR = "/";
private final static char SEP = '/';

public Display d;

public FileBrowser(){

currDirName = MEGA_ROOT;
fileName = null;
}

public String getFileName(){
return fileName;
}

public void startBrowsing(){
boolean isAPIAvailable = false;

if(System.getProperty("microedition.io.FileConnection.version") != null){
isAPIAvailable = true;
try{
showCurrDir();


}catch(SecurityException se){}
catch(Exception e){}
}
else{ }
}
public void showCurrDir(){
Enumeration e;
FileConnection currDir = null;
List browser;
try{
if(MEGA_ROOT.equals(currDirName)){
e = FileSystemRegistry.listRoots();
browser = new List(currDirName, List.IMPLICIT);

}
else{
currDir = (FileConnection)Connector.open("file://localhost/" + currDirName);
e = currDir.list();
browser = new List(currDirName, List.IMPLICIT);
browser.append(UP_DIR, null);
}
while(e.hasMoreElements()){
fileName = (String)e.nextElement();
if(fileName.charAt(fileName.length()-1) == SEP){
browser.append(fileName, null);

} else{
browser.append(fileName, null);
}
}
browser.setSelectCommand(open);
browser.addCommand(exit);
browser.setCommandListener(this);
if(currDir != null){
currDir.close();

}

} catch(IOException ioe){}

}
public void commandAction(Command c, Displayable d){
if(c == open){
List curr = (List)d;
final String currFile = curr.getString(curr.getSelectedIndex());
new Thread(new Runnable()
{
public void run()
{
if (currFile.endsWith(SEP_STR) ||
currFile.equals(UP_DIR)) {
traverseDirectory(currFile);
}
else {
openFile(currFile);
}
}
}).start();
}
else if (c == back)
{
showCurrDir();
}

}
public void traverseDirectory(String fileName){
if(currDirName.equals(MEGA_ROOT)){
if(fileName.equals(UP_DIR)){
return;
}
currDirName = fileName;
} else if(fileName.equals(UP_DIR)){
int i = currDirName.lastIndexOf(SEP, currDirName.length()-2);
if(i != -1){
currDirName = currDirName.substring(0, i+1);
}
else{
currDirName = MEGA_ROOT;
}
}
showCurrDir();
}
public void openFile(String fileName){
try{
FileConnection fc = (FileConnection) Connector.open("file://localhost/" + currDirName + fileName);
if(!fc.exists()){
throw new IOException("File does not exists");
}
InputStream fileIS = fc.openDataInputStream();
byte[] b = new byte[1024];

int length = fileIS.read(b, 0, 1024);
fileIS.close();
fc.close();
TextBox tb = new TextBox("View File: " + fileName, null, 1024, TextField.ANY | TextField.UNEDITABLE);
tb.addCommand(back);
tb.addCommand(exit);
tb.setCommandListener(this);

if (length > 0)
{
tb.setString(new String(b, 0, length));
}
} catch(Exception e){}

}

}

and here is the function in the snippet of mainInterface class
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == open){
start the filebrowsing screen
}
}

anyone can help?
thx in advanced

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07

This is a LWUIT forum not a MIDP hi level UI forum.
You can do these things quite easily in LWUIT, see our demos.