How to create a Splash Screen in eSWT
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|write your reason here}}.
Article Metadata
Tested with
Devices(s): S60 3rd Ed. FP2 SDK
Compatibility
Platform(s): S60 3rd Edition, FP2
Article
Keywords: eSWT, Splash Screen, Shell, FormLayout
Created: gorkem.ercan
(26 Nov 2008)
Last edited: hamishwillee
(10 May 2012)
Contents |
Overview
This snippet shows how to create a splash screen using eSWT. Example implements the splash screen as a window that is not maximized. Shows an image and also updates a ProgressBar. Follow the video link below to see how it works
Source file
import javax.microedition.midlet.MIDlet;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class SplashScreenDemo extends MIDlet implements Runnable, SelectionListener {
// A handle to the eSWT UI thread created by this MIDlet.
private Thread UIThread;
// The eSWT Display created by this MIDlet in the eSWT UI thread.
// When this is created the MIDlet gets connected to the native UI
// functionality and eSWT UI toolkit is initialised for it.
private Display display;
// A Shell widget created by this MIDlet.
private Shell shell;
// A boolean to set when the event loop should exit.
private boolean exiting = false;
public void startApp() {
// Create the eSWT UI thread.
if(UIThread == null) {
UIThread = new Thread(this);
UIThread.start();
}
}
public void pauseApp() {
// Here we could reduce the resources but we should keep the Display
// instance and the eSWT UI Thread.
}
// destroyApp is called when the MIDlet is terminated from the task list
// with the clear key, or the end key is pressed when the MIDlet is focused.
// It might also be called when the system needs to close applications
// e.g. in low memory conditions.
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) {
}
}
// This method can be called from any thread to make the event loop to exit.
void exitEventLoop() {
exiting = true;
Display.getDefault().wake();
}
// The eSWT UI Thread.
public void run() {
// Create the Display.
display = new Display();
shell = new Shell(display);
Rectangle screenSize = display.getBounds();
final Image image = new Image(display, screenSize.width-50, screenSize.height/2);
GC gc = new GC(image);
gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
gc.fillRectangle(image.getBounds());
gc.drawText("Splash Screen", 0, 5);
gc.dispose();
final Shell splash = new Shell(shell, SWT.ON_TOP);
final ProgressBar bar = new ProgressBar(splash, SWT.SMOOTH);
Label label = new Label(splash, SWT.BORDER);
label.setImage(image);
FormLayout layout = new FormLayout();
splash.setLayout(layout);
FormData labelData = new FormData ();
labelData.right = new FormAttachment (100, 0);
labelData.bottom = new FormAttachment (100, 0);
label.setLayoutData(labelData);
FormData progressData = new FormData ();
progressData.left = new FormAttachment (0, 5);
progressData.right = new FormAttachment (100, -5);
progressData.bottom = new FormAttachment (100, -5);
bar.setLayoutData(progressData);
bar.setMaximum(10);
splash.pack();
Rectangle splashRect = splash.getBounds();
Rectangle displayRect = display.getBounds();
int x = (displayRect.width - splashRect.width) / 2;
int y = (displayRect.height - splashRect.height) / 2;
splash.setLocation(x, y);
splash.open();
display.asyncExec(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
bar.setSelection(i+1);
bar.update();
try {Thread.sleep(1000);} catch (InterruptedException e) {
}
}
splash.close();
image.dispose();
shell.setVisible(true);
}
});
// 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) {
// Exit command selected, exit the event loop.
exitEventLoop();
}
}
Postconditions
The splash screen will appear for 10 secs and update the ProgressBar every second. And will make the main application screen visible.


24 Sep
2009
The very first and appealing requirement of any company or any developer when they launch application is to display s splash screen at the starting of the application for some time that gives user company info,Developer info,contact info etc.So this article is dedicated to all developers who want to make their application presentable. The code snippet given in this article is using the widget library that has been embedded into the j2me and their specific widget package imported to use 3SWT.
Article gives basis components that make splash screen like an background image on specific size of window that is not re-sizable and progress bar,indication for application staring time.
This article satisfies the need of all developers who want to make very interactive splash screen for their application. Code given works perfectly.