×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Using eSWT FileDialog in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
jarmlaht (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Adding missing translation link)
(31 intermediate revisions by 8 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:MIDP 2.0]][[Category:File Connection and PIM API (JSR-75)]][[Category:ESWT API]][[Category:Code Examples]][[Category:S60 3rd Edition FP2]][[Category:S60 5th Edition]][[Category:Symbian^3]][[Category:Symbian Anna]][[Category:Nokia Belle]][[Category:Symbian]][[Category:Files/Data]]
__NOEDITSECTION__
+
{{VersionHint}}
  
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
+
{{ArticleMetaData <!-- v1.2 -->
|-
+
|sourcecode= [[Media:FileDialogMIDletSource.zip]]
|'''ID''' || &nbsp;
+
|installfile= [[Media:FileDialogMIDletBinaries.zip]]
|'''Creation date''' || May 7, 2008
+
|devices= S60 3rd Ed. FP2 SDK
|-
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|'''Platform''' || S60 3rd Edition, FP2
+
|platform= S60 3rd Edition, FP2
|'''Tested on devices''' || S60 3rd Ed. FP2 SDK
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|-
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|'''Category''' || Java
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|'''Subcategory''' || eSWT, UI
+
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|}
+
|keywords= eSWT, FileDialog
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20080507
 +
|author= [[User:Jarmlaht]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= eSWT, UI
 +
|id= CS000946
 +
}}
 +
 
 +
==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. The following section contains the complete code for compiling and running the example application.
  
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
+
<gallery widths=240px heights=320px>
|-
+
File:E52ori.jpg| Start Screen on E52
|'''Keywords (APIs, classes, methods, functions):''' eSWT, FileDialog
+
File:E52toplevel.jpg| Selection window
|}
+
File:E52depth.jpg| Root of phone memory on E52
 
+
</gallery>
==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.
 
  
 +
<gallery widths=180px heights=320px>
 +
File:808toplevel.jpg| Selection window
 +
File:808depth.jpg|Root of phone memory on PureView 808
 +
</gallery>
 +
==Source code==
  
 
<code java>
 
<code java>
Line 44: Line 64:
 
     private Command exitCommand;
 
     private Command exitCommand;
 
     private Text text;
 
     private Text text;
     private String textString = "Hello world!";
+
     private String textString = "FileDialogMIDlet";
  
 
     public void startApp() {
 
     public void startApp() {
Line 93: Line 113:
 
         // 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 124: Line 144:
 
         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 canceled.
                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 or if the method is invoked on a directory.
 +
                // Note: 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);
 
             }
 
             }
        } catch (IOException ioe) {
+
             formShell.redraw();      
            textString = "IOException: " + ioe.getMessage();
+
             text.setText(textString);
+
 
         }
 
         }
        catch (SecurityException se) {
 
            textString = "SecurityException: " + se.getMessage();
 
            text.setText(textString);
 
        }
 
        formShell.redraw();
 
 
     }
 
     }
 
}
 
}
 
</code>
 
</code>
  
 +
==Postconditions==
 +
 +
When a {{Icode|FileDialog}} is opened and a file is selected in it, the file size is shown on the screen.
  
 
==See also==
 
==See also==
  
* [http://wiki.forum.nokia.com/index.php/ESWT eSWT]
+
* [[ESWT|eSWT]]
 
+
<!-- Translation --> [[pt:Como usar caixas de diálogo eSWT em Java ME]]
[[Category:S60 3rd Edition, Feature Pack 2]][[Category:Java ME]][[Category:MIDP 2.0]][[Category:eSWT]][[Category:File Connection and PIM API (JSR-75)]]
+

Revision as of 10:32, 17 September 2012

Contents

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

Article Metadata
Code ExampleTested with
Devices(s): S60 3rd Ed. FP2 SDK
CompatibilityArticle
Keywords: eSWT, FileDialog
Created: jarmlaht (07 May 2008)
Last edited: hamishwillee (17 Sep 2012)

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. The following section contains the complete code for compiling and running the example application.


Source code

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 = "FileDialogMIDlet";
 
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 canceled.
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 or if the method is invoked on a directory.
// Note: 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();
}
}
}

Postconditions

When a FileDialog is opened and a file is selected in it, the file size is shown on the screen.

See also

316 page views in the last 30 days.
×