RSS

Search Engine

Showing posts with label SWT - Standard Widget Toolkit Tutorial. Show all posts
Showing posts with label SWT - Standard Widget Toolkit Tutorial. Show all posts

Friday, June 11, 2010

SWT - Standard Widget Toolkit

SWT is the UI library used by Eclipse. SWT provides the some rich UI components on several platforms. The native widgets of the OS are accessed by the SWT framework via JNI. If a widget is not available on a certain platform, SWT emulates the unavailable widget.

SWT is from design similar to AWT but AWT does not provide widgets if they are available on all platform, therefore the AWT widget library is very small.

The key components of SWT applications are the classes "Display" and "Shell". A Shell represents a window. Displays are responsible from managing event loops and controlling communication between the UI thread and other threads. Every SWT application requires at least one Display and one or more Shell instances. The main shell gets as a default parameter a Display as construtor argument.

Your first SWT Application

Create a new Java project "de.vogella.swt.first" and add the "swt.jar" to the build path. Create the following class.

   
package de.vogella.swt.first;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FirstSWTApplication {

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

If you run this application you will receive an empty shell.

Labels and Texts

Create a new Java project "de.vogella.swt.simple" with the swt.jar in the classpath. Create the following class.

   
package de.vogella.swt.first;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FirstSWTApplication {

public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

Table

Create a new Java project "de.vogella.swt.table" with the swt.jar in the classpath. Create the following class.

			

package de.vogella.swt.table;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class SWTTable {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);

shell.setLayout(new GridLayout());


Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 200;
table.setLayoutData(data);

String[] titles = { "First Name", "Last Name", "Age" };
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(titles[i]);
table.getColumn(i).pack();
}

for (int i = 0 ; i<= 50 ; i++){
TableItem item = new TableItem(table, SWT.NONE);
item.setText (0, "Person " +1 );
item.setText (1, "LastName " +1 );
item.setText (2, String.valueOf(i));
}

for (int i=0; i table.getColumn (i).pack ();
}
shell.pack ();
shell.open ();

shell.open();
while (!display.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}

If you run this application a table will be displayed.

Thank you

Thank you for practicing with this tutorial.

I maintain this tutorial in my private time. If you like the information please help me by donating or by recommending this tutorial to other people.