What is Standard Widget Toolkit for Eclipse (SWT) ?


Here I am at my new workplace. Next 8 months I will be working in Eclipse team, on migration of SWT  from GTK2 to GTK3.

If these words does not tell you much, don’t worry, I am pretty much in the same situation, but what I am going to do in the next couple of days is to explore this issue as much as I can, and  write down my findings here.

Let’s start with SWT.  My first stop in finding info about SWT was here: http://www.eclipsepluginsite.com/swt.html

After reading  introductory chapter,  I have  found out that SWT stands for   Standard Widget Toolkit.  As I have understood from the reading, SWT is a collection of tools, intended for creating stand alone java applications or eclipse plugins  with basic UI components (such as buttons, trees etc). What is  special about SWT, is that it uses native OS components, so when you build application with SWT it feels like this application is intended for the particular OS.

I decided to build a simple application using one of the examples provided in the reading.

Simple SWT Application

/*
 *  Name: BlogButton.java
 *  Description:  SWT  button on click changes text and background color
 *
 *  Author: Anatoly Spektor
 *  Date: May 14,2012
 * */

import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.*;

public class BlogButton {

public static void main(String[] args) {
 // creating new object of Display myDisplay
 //in SWT Display manages connection between app and OS
 final Display myDisplay = new Display();
 // creating instance of shell - which is actual window
 Shell myShell = new Shell(myDisplay);

// setting window title
 myShell.setText("Window Title");
 //setting size
 myShell.setSize(400,400);
 // setting layout
 myShell.setLayout(new FillLayout());
 // creating button object in myShell
 // passing PUSH behaviour
 final Button button = new Button(myShell, SWT.PUSH);
 // passing button label
 button.setText("Show Blog URL");

// adding event listener to button
 button.addSelectionListener(new SelectionAdapter() {
 public void widgetSelected(SelectionEvent event) {
 //on selected change button label
 button.setText("myprogrammingblog.com");
 // set text color to red
 button.setForeground(myDisplay.getSystemColor(SWT.COLOR_RED));

}
 });

// open - pushes shell on the top of drawing order and makes it visible
 myShell.open();

// checking if shell is Disposed or not
 while (!myShell.isDisposed()) {
 // check if there is work to do for this shell and if not
 // put it in sleep mode so no CPU is consumed
 if (!myDisplay.readAndDispatch()) myDisplay.sleep();
 }
 // disposing an object
 myDisplay.dispose();
 }
 }

Output:

Before Click:
SWT button before click

After Click:

SWT button after click snapshot

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

About these ads

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module. Currently, I am employed as Software Engineer in Red Hat.
This entry was posted in Programming Languages, Tutorials, Scripts, open-source, Java, Eclipse Development, SWT, Fedora and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s