Developing a Desktop Widget in JAVA

This widget will be developed using Eclipse SWT. Hence assuming you already have java and eclipse, you will also require to download Eclipse SWT zip. Which you can download on,

Though to develop this particular widget you will not require a expertise knowledge in Eclipse SWT. You can go through,

in order to get started with SWT. But it’s not really necessary here and you can still go through this even if you are an absolute starter in SWT. And still you will have a fair knowledge on SWT at the end of this post. If you wanna have more of SWT, you can go through,

Which should cover all what you need to know about SWT. Let’s get to the business end of life.

1. Open up your eclipse and create new project and name it with whatever name you wish (In my case it was “WidgetPro”).

2. Now again go to file menu and select import. And under general select “select archivecheck box (it will not be selected by default) and browse to point the downloaded “swt-3.x*.zip”.

3. Create a new folder inside your project’s src (source). And name it “images”. Download the following images and place them inside the folder which you just created.

N.B. Your widget will take the shape of the first image.

4. Now you can create new class, name it “GSearchWidget”. And paste the following code into it.

import java.awt.TextField;

import org.eclipse.swt.*;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;

import java.awt.Frame;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

 * @author Lonely Coder
 * @date 12/05/2009
 * @since 3.2
public class GSearchWidget {
	public static void main(String[] args) {

	final Display display = new Display();
	String currentDirectory = System.getProperty("user.dir");
	final Image image = new Image(display, "images/searchBox.png");
	final Image icon = new Image(display, "images/ico.png");
	Color green = display.getSystemColor(SWT.COLOR_DARK_GREEN);
	GC gc = new GC(image);
	// final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
	final Shell shell = new Shell(display, SWT.NO_TRIM);
	shell.setText("G Search.");
	display.setAppName("G App.");
	shell.setLocation(800, 20);
	shell.setToolTipText("Lonely Coder Labs.");
	Region region = new Region();
	final ImageData imageData = image.getImageData();

	if (imageData.alphaData != null) {
		Rectangle pixel = new Rectangle(0, 0, 1, 1);
		for (int y = 0; y < imageData.height; y++) {
			for (int x = 0; x < imageData.width; x++) {
				if (imageData.getAlpha(x, y) == 255) {
					pixel.x = imageData.x + x;
					pixel.y = imageData.y + y;
	} else {
		ImageData mask = imageData.getTransparencyMask();
		Rectangle pixel = new Rectangle(0, 0, 1, 1);
		for (int y = 0; y < mask.height; y++) {
			for (int x = 0; x < mask.width; x++) {
				if (mask.getPixel(x, y) != 0) {
					pixel.x = imageData.x + x;
					pixel.y = imageData.y + y;

	Listener l = new Listener() {
	int startX, startY;

	public void handleEvent(Event e) {
		if (e.type == SWT.KeyDown && e.character == SWT.ESC) {
		if (e.type == SWT.MouseDown && e.button == 1) {
			startX = e.x;
			startY = e.y;
		if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
			Point p = shell.toDisplay(e.x, e.y);
			p.x -= startX;
			p.y -= startY;
		if (e.type == SWT.Paint) {
			e.gc.drawImage(image, imageData.x, imageData.y);
		shell.addListener(SWT.KeyDown, l);
		shell.addListener(SWT.MouseDown, l);
		shell.addListener(SWT.MouseMove, l);
		shell.addListener(SWT.Paint, l);

		shell.setSize(imageData.x + imageData.width, imageData.y
				+ imageData.height);

		final Text txt = new Text(shell, SWT.WRAP | SWT.BORDER);
		txt.setBounds(20, 20, 100, 20);
		txt.addListener (SWT.Verify, new Listener () {
			public void handleEvent (Event e) {
				String string = e.text;

		Button go = new Button(shell, SWT.PUSH);
		go.setLocation(125, 18);

	go.addListener(SWT.Selection, new Listener() {
	public void handleEvent(Event e) {
	//System.out.println("Clicked" + txt.getText());
	try {
	URL query = new URL("
        search?q=" + txt.getText());
	Runtime.getRuntime().exec("rundll32 url.dll,File
        ProtocolHandler " + query);
	} catch (MalformedURLException mue) {
		System.out.println("Ouch - a MalformedURLException happened.");
	} catch(IOException io){
		//throw Exception("");

		Button b = new Button(shell, SWT.PUSH);
		b.setLocation(190, 5);
		b.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())

That’s it you have just completed the widget. Hit the run button and enjoy it. This widget is a good example of a desktop application interacting directly with a web page. To make it more interesting I have also given some transparency. With include and exclude features of SWT developing transparent applications becomes seamlessly easy. Here Firefox was opened explicitly. Since, in ma case, the default browser was found to be Internet Explorer and I wanted to view results in Firefox.

Nice to be back with a post after a while. Hope you enjoyed it, thank you.