February 19, 2008

SWT - Label, Text, Button, List, Table

Reference: http://www.cs.umanitoba.ca/~eclipse/

The Eclipse IDEs user interface in made with SWT (Standard Widget Toolkit). This is basic program in SWT:

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;

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

Label label = new Label(shell, SWT.NONE);
label.setText("Hello, World!");

shell.pack();
label.pack();

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


You can add various components in SWT in the following manner:
Label

Label label = new Label(shell, SWT.NONE);
label.setSize(100,20);
label.setLocation(30,150);
label.setBackground(new Color(display,200,111,50));
label.setText("Hello World");


Text

Text text = new Text(shell, SWT.BORDER);
text.setText("Hello World");
text.setBounds(10,10,200,20);
text.setTextLimit(30);


Button

Button button = new Button(shell,SWT.PUSH);
button.setText("Hello World");
button.setLocation(0,0);
button.setSize(100,20);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Button was clicked");
}
});


List

List list = new List(shell, SWT.MULTI | SWT.H_SCROLL);
list.setItems(new String[] {"Hello","World"});
list.add("SWT");
list.setBounds(0,0,60,100);

list.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
System.out.print(list.getSelection()[0]);
}

public void mouseUp(MouseEvent e) {
System.out.println(" selected");
}
});


Table

Table table = new Table(shell, SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setBounds(10,180,270,80);

TableColumn first = new TableColumn(table,SWT.LEFT);
first.setResizable(true);
first.setText("First");
first.setWidth(80);

TableColumn second = new TableColumn(table,SWT.CENTER);
second.setText("Second");
second.setWidth(80);

String[] numbers = new String[] {"One","Two"};

TableItem firstItem = new TableItem(table,SWT.NONE);
firstItem.setText(numbers);

No comments: