<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Eduzine© - Eclipse</title>
    <link>http://eduzine.edujini-labs.com/</link>
    <description>Articles from Edujini Team</description>
    <dc:language>en</dc:language>
    <admin:errorReportsTo rdf:resource="mailto:" />
    <generator>Serendipity 0.8.2 - http://www.s9y.org/</generator>
    <pubDate>Thu, 26 Jun 2008 20:59:33 GMT</pubDate>

    <image>
        <url>http://www.edujini-labs.com/images/newmasthead.gif</url>
        <title>RSS: Eduzine© - Eclipse - Articles from Edujini Team</title>
        <link>http://eduzine.edujini-labs.com/</link>
        <width></width>
        <height></height>
    </image>
<item>
    <title>Standard Widget Toolkit: Part 1 - Hello, World!</title>
    <link>http://eduzine.edujini-labs.com/archives/37-Standard-Widget-Toolkit-Part-1-Hello,-World!.html</link>
<category>Eclipse</category><category>SWT</category><category>&quot;Hello, World!&quot;</category>    <comments>http://eduzine.edujini-labs.com/archives/37-Standard-Widget-Toolkit-Part-1-Hello,-World!.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=37</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=37</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
With this article, we start a series on Standard Widget Toolkit (SWT) and Eclipse Plug-in Framework at Eduzine&lt;sup&gt;©&lt;/sup&gt;. It is assumed that you have already heard about Eclipse and you are gungho about exploring it and may be getting into all its details.
&lt;/p&gt;

&lt;p&gt;
SWT is the base UI toolkit to create the UI, mainly the native widgets. Then there is JFace, an abstraction that sits on top of JFace (a large part of it) and adds power to native widgets. And finally, there is Eclipse Plug-in Framework that helps creating applications in a modular fashion. Default implementation of the Eclipse Workbench (the main UI cockpit) uses SWT and JFace for its UI.
&lt;/p&gt;

&lt;p&gt;
Following the tradition started few decades back, we start with &amp;quot;Hello, World!&amp;quot; application.
&lt;/p&gt;

&lt;h2&gt;Downloads&lt;/h2&gt;

&lt;p&gt;
The downloads are available in the &lt;a href=&quot;http://downloads.edujini-labs.com&quot;&gt;Downloads&lt;/a&gt; area in the &lt;a href=&quot;http://downloads.edujini-labs.com/index.php?act=category&amp;id=28&quot;&gt;Eclise Standard Widget Toolkit&lt;/a&gt; category.
&lt;/p&gt;

 
&lt;h2&gt;Pre-requisites&lt;/h2&gt;

&lt;p&gt;
To get started, the following pre-requisites would be considered:
&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;You know Java programming langauge.&lt;/li&gt;
  &lt;li&gt;You have JDK (not only JRE) installed on your machine (from &lt;a href=&quot;http://java.sun.com&quot;&gt;http://java.sun.com&lt;/a&gt;).&lt;/li&gt;
  &lt;li&gt;You have Eclipse installed on your machine (from &lt;a href=&quot;http://www.eclipse.org&quot;&gt;http://www.eclipse.org&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Case Study&lt;/h2&gt;

&lt;p&gt;
We will create our final application in multiple steps. Here's the brief overview of these steps:
&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#gvaish37-step1&quot;&gt;Step 1&lt;/a&gt;: Create a window that contains a text that reads &amp;quot;Hello, World!&amp;quot;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gvaish37-step2&quot;&gt;Step 2&lt;/a&gt;: Create a window that has a button on click of which we get launch other window with a text that reads, &amp;quot;Hello, World!&amp;quot;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
And the &amp;quot;top level window&amp;quot; with a &amp;quot;Title Bar&amp;quot;, &amp;quot;Minimize&amp;quot;, &amp;quot;Maximize&amp;quot; and &amp;quot;Close&amp;quot; buttons - the one that interacts with the Desktop or &amp;quot;Window Manager&amp;quot; - is known as &lt;code&gt;Shell&lt;/code&gt; in SWT.
&lt;/p&gt;

&lt;h2&gt;Getting Started - Creating Project&lt;/h2&gt;

&lt;p&gt;
We start by creating a project using Eclipse IDE. Follow these steps to create and configure a simple Java application project.
&lt;/p&gt;

&lt;p&gt;
Step 1: In the &amp;quot;New Project&amp;quot; wizard, select &amp;quot;Java Project&amp;quot;. Click Next.
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/eclipse/swt/37.Eclipse.SWT.Hello.World-01.New.Project.jpg&quot; alt=&quot;Eduzine: Eclipse: SWT: Project&quot; title=&quot;Eduzine: Eclipse: SWT: Project&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
Step 2: Give the name of the project - &amp;quot;HelloWorldSWT&amp;quot;. Click Next.
&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/eclipse/swt/37.Eclipse.SWT.Hello.World-02.New.Project.Name.jpg&quot; alt=&quot;Eduzine: Eclipse: SWT: Project&quot; title=&quot;Eduzine: Eclipse: SWT: Project&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
Step 3: Go to the &amp;quot;Libraries&amp;quot; Tab. Click &amp;quot;Add External Jars...&amp;quot; button.
&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/eclipse/swt/37.Eclipse.SWT.Hello.World-03.Libraries.jpg&quot; alt=&quot;Eduzine: Eclipse: SWT: Project&quot; title=&quot;Eduzine: Eclipse: SWT: Project&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
Step 4: Browse to the &amp;quot;plugins&amp;quot; folder within the folder where Eclipse is installed. Add references to &lt;code&gt;org.eclipse.swt_&amp;lt;version&amp;gt;.jar&lt;/code&gt; and &lt;code&gt;org.eclipse.swt.win32.win32.x86_&amp;lt;version&amp;gt;.jar&lt;/code&gt;. Click Finish.
&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/eclipse/swt/37.Eclipse.SWT.Hello.World-04.Updated.jpg&quot; alt=&quot;Eduzine: Eclipse: SWT: Project&quot; title=&quot;Eduzine: Eclipse: SWT: Project&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Entry Point&lt;/h2&gt;

&lt;p&gt;
Now that we have the project, let us create a class &lt;code&gt;HelloWorldClass&lt;/code&gt; that will act as the entry point (through the &lt;code&gt;main&lt;/code&gt; method) for our application.
&lt;/p&gt;
&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/eclipse/swt/37.Eclipse.SWT.Hello.World-05.HelloWorldClass.jpg&quot; alt=&quot;Eduzine: Eclipse: SWT: Add HelloWorldClass&quot; title=&quot;Eduzine: Eclipse: SWT: Add HelloWorldClass&quot; /&gt;
&lt;/p&gt;

&lt;a name=&quot;gvaish37-step1&quot;&gt;&lt;h2&gt;Implementation: Step 1&lt;/h2&gt;&lt;/a&gt;

&lt;p&gt;
Let's get started with the implementation. The first thing that we will ever need to create the UI is an instance of &lt;code&gt;org.eclipse.swt.widgets.Display&lt;/code&gt;. The top-level window, as discussed earlier, is an instance of &lt;code&gt;org.eclipse.swt.widgets.Shell&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Update the code for &lt;code&gt;HelloWorldClass&lt;/code&gt; to as follows:
&lt;/p&gt;

&lt;pre&gt;package com.edujinilabs.eduzine.eclipse.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 &lt;strong&gt; Part of the Eduzine (http://eduzine.edujini-labs.com) Articles.
 &lt;/strong&gt; Downloads available at http://downloads.edujini-labs.com
 &lt;strong&gt; (C) 2008, Edujini Labs Pvt Ltd.
 &lt;/strong&gt; http://www.edujini-labs.com
 */
public class HelloWorldClass
{
  public static void main(String[] args)
  {
    Display display = new Display();
    Shell mainWindow = new Shell(display, SWT.DIALOG_TRIM);

    mainWindow.setText(&amp;quot;Eduzine: SWT Hello World&amp;quot;);
    mainWindow.open();
  }
}
&lt;/pre&gt;

&lt;p&gt;
Right click on the file and select &amp;quot;Run As...&amp;quot; -&amp;gt; &amp;quot;SWT Application&amp;quot;.
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/eclipse/swt/37.Eclipse.SWT.Hello.World-06.Launch.jpg&quot; alt=&quot;Eduzine: Eclipse: SWT: Launch Main Class&quot; title=&quot;Eduzine: Eclipse: SWT: Launch Main Class&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
While launching, keep a close eye on the monitor. What do you see?
&lt;/p&gt;

&lt;p&gt;
Ouch! The new windows seems to be launched but also lost somewhere. &lt;img src=&quot;http://eduzine.edujini-labs.com/templates/default/img/emoticons/sad.png&quot; alt=&quot;:-(&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
Well, if that's the case... you are on absolutely correct track! &lt;img src=&quot;http://eduzine.edujini-labs.com/templates/default/img/emoticons/smile.png&quot; alt=&quot;:-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
The reason for this behaviour is that the &lt;code&gt;open&lt;/code&gt; method is a non-blocking call meaning that it opens up the window and returns immediately. And in our code, nobody stops the controls to get transferred past-end of the &lt;code&gt;main&lt;/code&gt; causing the application to terminate.
&lt;/p&gt;

&lt;p&gt;
So, the next step is to add the code that does the following:
&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Waits indefinitely, only to be terminated if we click the close [X] button on the title-bar of the main window.&lt;/li&gt;
  &lt;li&gt;Does not use CPU cycles if no activity is to be performed.&lt;/li&gt;
  &lt;li&gt;Respond to events, if there is any activity and again go for indefinite wait.&lt;/li&gt;
  &lt;li&gt;On closing the main window, it frees up the resources used, especially OS resources like Font, Color, Pen/Brush etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
The loop that does the first-three steps mentioned above is known as &lt;em&gt;event loop&lt;/em&gt;.
&lt;/p&gt;

&lt;p&gt;
Update the &lt;code&gt;main&lt;/code&gt; method to the following code:
&lt;/p&gt;

&lt;pre&gt;public class HelloWorldClass
{
  public static void main(String[] args)
  {
    Display display = new Display();
    Shell mainWindow = new Shell(display, SWT.DIALOG_TRIM);

    mainWindow.setText(&amp;quot;Eduzine: SWT Hello World&amp;quot;);
    mainWindow.open();

    &lt;b&gt;while(!mainWindow.isDisposed())
    {
      if(!display.readAndDispatch())
      {
        display.sleep();
      }
    }
    display.dispose();&lt;/b&gt;
  }
}
&lt;/pre&gt;

&lt;p&gt;
Launch the application again. Not posting the screenshot since it's a huge window. &lt;img src=&quot;http://eduzine.edujini-labs.com/templates/default/img/emoticons/smile.png&quot; alt=&quot;:-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;
The next step is to add a text on the window that reads &amp;quot;Hello, World!&amp;quot;. We make use of the Widget named &lt;code&gt;Label&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
The constructors of all basic widgets require the information about the parent container in which they will be placed - the containers referred to as &lt;code&gt;Composite&lt;/code&gt; widgets. The second parameter to the constructor is a number (integer) representing the styles. We will explore the styles soon. So, what are we waiting for? Let's update the code... And ah! We shall resize the window to a smaller size.
&lt;/p&gt;

&lt;pre&gt;public class HelloWorldClass
{
  public static void main(String[] args)
  {
    Display display = new Display();
    Shell mainWindow = new Shell(display, SWT.DIALOG_TRIM);

    &lt;b&gt;Label hwLabel = new Label(mainWindow, SWT.NONE);
    hwLabel.setText(&amp;quot;Hello, World!&amp;quot;);&lt;/b&gt;

    mainWindow.setText(&amp;quot;Eduzine: SWT Hello World&amp;quot;);
    &lt;b&gt;mainWindow.setSize(300, 200);&lt;/b&gt;
    mainWindow.open();

    while(!mainWindow.isDisposed())
    {
      if(!display.readAndDispatch())
      {
        display.sleep();
      }
    }
    display.dispose();
  }
}
&lt;/pre&gt;

&lt;p&gt;
Things look much better now except only that we are yet, unable to see the &lt;code&gt;Label&lt;/code&gt;.
&lt;/p&gt;

&lt;h2&gt;Layout Manager&lt;/h2&gt;

&lt;p&gt;
All &lt;code&gt;Composite&lt;/code&gt; widgets use a layout manager - technically a subclass of &lt;code&gt;org.eclipse.swt.layout.GridLayout&lt;/code&gt; - to layout the child widgets within themselves.
&lt;/p&gt;

&lt;p&gt;
For our purpose, we make use of &lt;code&gt;org.eclipse.swt.layout.GridLayout&lt;/code&gt;. We explore it later on. Update the code to as follows:
&lt;/p&gt;

&lt;pre&gt;    Display display = new Display();
    Shell mainWindow = new Shell(display, SWT.DIALOG_TRIM);

    &lt;b&gt;GridLayout layout = new GridLayout();
    mainWindow.setLayout(layout);&lt;/b&gt;

    Label hwLabel = new Label(mainWindow, SWT.NONE);
    hwLabel.setText(&amp;quot;Hello, World!&amp;quot;);
&lt;/pre&gt;

&lt;p&gt;
Launch the application! Voila! Got the &amp;quot;Hello, World!&amp;quot;. The expected output is as below:
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/eclipse/swt/37.Eclipse.SWT.Hello.World-07-Layout.jpg&quot; alt=&quot;Eduzine: Eclipse: SWT: Specify Layout, Final Output&quot; title=&quot;Eduzine: Eclipse: SWT: Specify Layout, Final Output&quot; /&gt;
&lt;/p&gt;

&lt;a name=&quot;gvaish37-step2&quot;&gt;&lt;h2&gt;Implementation - Step 2&lt;/h2&gt;&lt;/a&gt;

&lt;p&gt;
Now that we've got a simple but working application, let's move on to the next step, making it interactive. We replace the &lt;code&gt;Label&lt;/code&gt; with a &lt;code&gt;org.eclipse.swt.widgets.Button&lt;/code&gt; and popup a new window that reads, &amp;quot;Hello, World!&amp;quot;
&lt;/p&gt;

&lt;p&gt;
Firstly, we replace &lt;code&gt;Label&lt;/code&gt; with &lt;code&gt;Button&lt;/code&gt;. Update the code to as follows:
&lt;/p&gt;

&lt;pre&gt;package com.edujinilabs.eduzine.eclipse.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
&lt;b&gt;import org.eclipse.swt.widgets.Button;&lt;/b&gt;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HelloWorldClass
{
  public static void main(String[] args)
  {
    Display display = new Display();
    Shell mainWindow = new Shell(display, SWT.DIALOG_TRIM);

    GridLayout layout = new GridLayout();
    mainWindow.setLayout(layout);

    &lt;b&gt;Button hwBtn = new Button(mainWindow, SWT.NONE);
    hwBtn.setText(&amp;quot;Say Hello&amp;quot;);&lt;/b&gt;

    mainWindow.setText(&amp;quot;Eduzine: SWT Hello World&amp;quot;);
    mainWindow.setSize(300, 200);
    mainWindow.open();

    while(!mainWindow.isDisposed())
    {
      if(!display.readAndDispatch())
      {
        display.sleep();
      }
    }
    display.dispose();
  }
}
&lt;/pre&gt;

&lt;p&gt;
Subsequently, we create a class &lt;code&gt;HelloWorldWindow&lt;/code&gt; with similar UI as created earlier. However, this time, the new class will have a new method &lt;code&gt;open&lt;/code&gt; that will actually launch the UI. The definition of the class is as follows:
&lt;/p&gt;

&lt;pre&gt;package com.edujinilabs.eduzine.eclipse.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class HelloWorldWindow
{
  private Shell window;

  public HelloWorldWindow(Shell parent)
  {
    this.window = new Shell(parent, SWT.DIALOG_TRIM);

    GridLayout layout = new GridLayout();
    window.setLayout(layout);

    Label hwBtn = new Label(window, SWT.NONE);
    hwBtn.setText(&amp;quot;Hello, World!&amp;quot;);

    window.setText(&amp;quot;Eduzine: Child&amp;quot;);
    window.setSize(200, 100);
  }

  public void open()
  {
    this.window.open();
  }
}
&lt;/pre&gt;

&lt;p&gt;
Now, the only missing link is to launch this window on the click of the button! What are we waiting for then? Let's do it! Update &lt;code&gt;HelloWorldClass&lt;/code&gt; to as follows:
&lt;/p&gt;

&lt;pre&gt;  public static void main(String[] args)
  {
    Display display = new Display();
    &lt;b&gt;final&lt;/b&gt; Shell mainWindow = new Shell(display, SWT.DIALOG_TRIM);

    GridLayout layout = new GridLayout();
    mainWindow.setLayout(layout);

    Button hwBtn = new Button(mainWindow, SWT.NONE);
    hwBtn.setText(&amp;quot;Say Hello&amp;quot;);

    &lt;b&gt;hwBtn.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e)
      {
        HelloWorldWindow w = new HelloWorldWindow(mainWindow);
        w.open();
      }
    });
    &lt;/b&gt;
    ...
  }
&lt;/pre&gt;

&lt;p&gt;
On launch, the output expected is follows (Hey! Don't forget to click the button).
&lt;/p&gt;

&lt;p&gt;
&lt;img src=&quot;http://eduzine.edujini-labs.com/uploads/eclipse/swt/37.Eclipse.SWT.Hello.World-08-Button-Launch.jpg&quot; alt=&quot;Eduzine: Eclipse: SWT: Child Window&quot; title=&quot;Eduzine: Eclipse: SWT: Child Window&quot; /&gt;
&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;
In this article, we learnt how to get started with Standard Widget Toolkit, the UI Toolkit created for initially for Eclipse and now used by millions of developers worldwide!
&lt;/p&gt;

&lt;p&gt;
In subsequent articles, we explore about SWT and Eclipse, and environment around them.
&lt;/p&gt;
    </content:encoded>
    <pubDate>Wed, 25 Jun 2008 15:58:16 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/37-guid.html</guid>
    </item>
</channel>
</rss>
