×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Using eSWT FileDialog in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
jarmlaht (Talk | contribs)
jarmlaht (Talk | contribs)
Line 94: Line 94:
 
         // Execute the eSWT event loop.  
 
         // Execute the eSWT event loop.  
 
         while(!exiting) {
 
         while(!exiting) {
        if(!display.readAndDispatch()) {
+
            if(!display.readAndDispatch()) {
            display.sleep();
+
                display.sleep();
 +
            }
 
         }
 
         }
    }
 
  
    // Clean up and destroy the MIDlet.
+
        // Clean up and destroy the MIDlet.
    display.dispose();
+
        display.dispose();
    notifyDestroyed();
+
        notifyDestroyed();
 
     }
 
     }
  
Line 125: Line 125:
 
         String fileName = fDialog.getFileName();
 
         String fileName = fDialog.getFileName();
 
         String path = "file:///" + filterPath + fileName;
 
         String path = "file:///" + filterPath + fileName;
         // path is for example: file:///C:/Data/Images/image.png, this can be used as file URL for FileConnection
+
         // path is for example: file:///C:/Data/Images/image.png, this can be used as file  
         try {
+
        // URL for FileConnection
             FileConnection fc = (FileConnection) Connector.open(path, Connector.READ);
+
         if (!path.equals("file:///")) {  
            if (!fc.isDirectory()) {
+
             // Path equals "file:///", if file dialog is cancelled.
                textString = "File size: " + fc.fileSize() + " B";
+
            try {
                text.setText(textString);             
+
                FileConnection fc = (FileConnection) Connector.open(path, Connector.READ);
 +
                if (!fc.isDirectory()) {
 +
                    textString = "File size: " + fc.fileSize() + " bytes";
 +
                    text.setText(textString);             
 +
                }
 +
            } catch (IOException ioe) {
 +
                // Thrown If an I/O error occurs, if the method is invoked on a directory.
 +
                // Must be caught, but is never thrown.
 +
                // It is not possible to select a directory in FileDialog.
 +
                textString = "IOException: " + ioe.getMessage();
 +
                text.setText(textString);
 
             }
 
             }
        } catch (IOException ioe) {
+
            catch (SecurityException se) {  
            textString = "IOException: " + ioe.getMessage();
+
                // Thrown if the security of the application does not have read access for
            text.setText(textString);
+
                // the file.
        }
+
                textString = "SecurityException: " + se.getMessage();
        catch (SecurityException se) {
+
                text.setText(textString);
             textString = "SecurityException: " + se.getMessage();
+
            }
            text.setText(textString);
+
             formShell.redraw();      
 
         }
 
         }
        formShell.redraw();
 
 
     }
 
     }
 
}
 
}

Revision as of 15:34, 12 May 2008


ID   Creation date May 7, 2008
Platform S60 3rd Edition, FP2 Tested on devices S60 3rd Ed. FP2 SDK
Category Java Subcategory eSWT, UI


Keywords (APIs, classes, methods, functions): eSWT, FileDialog

Overview

This code snippet demonstrates how to use eSWT's FileDialog class in Java ME. A FileDialog can be opened and when a file is selected, its size is shown on the screen.

Source

import javax.microedition.midlet.*;
import org.eclipse.ercp.swt.mobile.Command;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.FillLayout;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.Connector;
import java.io.IOException;
 
public class FileDialogMIDlet extends MIDlet implements Runnable, SelectionListener {
private Thread UIThread;
private Display display;
private Shell formShell;
private boolean exiting = false;
private Command fileCommand;
private Command exitCommand;
private Text text;
private String textString = "Hello world!";
 
public void startApp() {
// Create the eSWT UI thread.
if(UIThread == null) {
UIThread = new Thread(this);
UIThread.start();
}
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
// Make the event loop exit in the eSWT UI thread.
exitEventLoop();
// Wait for the eSWT UI thread to die.
try {
UIThread.join();
} catch(InterruptedException e) {
}
}
 
void exitEventLoop() {
exiting = true;
Display.getDefault().wake();
}
 
// The eSWT UI Thread.
public void run() {
display = new Display();
FillLayout fillLayout = new FillLayout();
formShell = new Shell(display);
formShell.setLayout(fillLayout);
formShell.open();
Composite form = new Composite(formShell, SWT.NONE);
form.setLayout(fillLayout);
text = new Text(form, SWT.READ_ONLY | SWT.WRAP);
text.setText(textString);
fileCommand = new Command(formShell, Command.SELECT, 0);
fileCommand.setText("Open FileDialog");
fileCommand.addSelectionListener(this);
exitCommand = new Command(formShell, Command.EXIT, 0);
exitCommand.setText("Exit");
exitCommand.addSelectionListener(this);
formShell.redraw();
 
// Execute the eSWT event loop.
while(!exiting) {
if(!display.readAndDispatch()) {
display.sleep();
}
}
 
// Clean up and destroy the MIDlet.
display.dispose();
notifyDestroyed();
}
 
public void widgetDefaultSelected(SelectionEvent e) {
}
 
public void widgetSelected(SelectionEvent e) {
if (e.widget.equals(fileCommand)) {
openFileDialog();
}
if (e.widget.equals(exitCommand)) {
// Exit command selected, exit the event loop.
exitEventLoop();
}
}
 
public void openFileDialog() {
FileDialog fDialog = new FileDialog(formShell);
fDialog.open();
String filterPath = fDialog.getFilterPath();
String temp = filterPath.replace('\\', '/'); // '\‘ characters are replaced with '/'
filterPath = temp;
String fileName = fDialog.getFileName();
String path = "file:///" + filterPath + fileName;
// path is for example: file:///C:/Data/Images/image.png, this can be used as file
// URL for FileConnection
if (!path.equals("file:///")) {
// Path equals "file:///", if file dialog is cancelled.
try {
FileConnection fc = (FileConnection) Connector.open(path, Connector.READ);
if (!fc.isDirectory()) {
textString = "File size: " + fc.fileSize() + " bytes";
text.setText(textString);
}
} catch (IOException ioe) {
// Thrown If an I/O error occurs, if the method is invoked on a directory.
// Must be caught, but is never thrown.
// It is not possible to select a directory in FileDialog.
textString = "IOException: " + ioe.getMessage();
text.setText(textString);
}
catch (SecurityException se) {
// Thrown if the security of the application does not have read access for
// the file.
textString = "SecurityException: " + se.getMessage();
text.setText(textString);
}
formShell.redraw();
}
}
}


See also

192 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×