SWT Sample
this sample code has some controls which has used in Devices,
Swt controls are Text, Label,combobox,Slider,Scale,GridLayout,GridData,Composite,Shell...
package pack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Text;
public class SWTSampleTest1 {
static Shell shell;
static Text t1,t2,t3;
static Label l1,l2,lbltest;
public static void main(String[] args) {
Display display = new Display();
shell= new Shell(display);
shell.setText("Testing...");
shell.setLayout(new FillLayout());
final Combo combobox;
final List list;
//ScrolledComposite scrollComposite=new ScrolledComposite(shell,SWT.NONE|SWT.H_SCROLL|SWT.V_SCROLL);
//Composite composite=new Composite(scrollComposite,SWT.NONE);
//scrollComposite.setContent(composite);
ScrolledComposite scomp=new ScrolledComposite(shell,SWT.H_SCROLL|SWT.V_SCROLL|SWT.NONE);
Composite composite=new Composite(scomp,SWT.NONE);
scomp.setContent(composite);
GridLayout gridlayout=new GridLayout();
gridlayout.numColumns=3;
composite.setLayout(gridlayout);
l1=new Label(composite,SWT.NULL|SWT.BOLD);
l1.setText("Name :");
t1=new Text(composite,SWT.BORDER);
GridData griddata=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata.horizontalSpan=2;
//griddata.horizontalIndent=1;
t1.setLayoutData(griddata);
l2=new Label(composite,SWT.NULL|SWT.BOLD);
l2.setText("MobileNo :");
t2=new Text(composite,SWT.BORDER);
GridData griddata1=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata1.horizontalSpan=2;
t2.setLayoutData(griddata1);
lbltest=new Label(composite,SWT.NULL);
lbltest.setText("AGE :");
t3=new Text(composite,SWT.BORDER);
griddata1=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata1.horizontalSpan=2;
t3.setLayoutData(griddata1);
final Button button=new Button(composite,SWT.NORMAL);
button.setText("Test");
GridData data1=new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
data1.horizontalSpan=2;
button.setLayoutData(data1);
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
System.out.println("Test ok ");
MessageBox messageBox =
new MessageBox(shell,SWT.OK|SWT.CLOSE|SWT.ICON_INFORMATION);
messageBox.setText("Information");
messageBox.setMessage("OK.... BYE...... Thank U!!!!");
if(messageBox.open()==SWT.OK){
String str="\n\t Name: "+t1.getText()+"\n\t Mobile no: "+t2.getText()+"\n\tAge: "+t3.getText();
System.out.println(str);
System.out.println("you came out of the shell. Thank U");
System.exit(0);
}
//System.exit(0);
}
});
final Slider slider =
new Slider(composite,SWT.HORIZONTAL);
slider.setMinimum(0);
slider.setMaximum(100);
slider.setIncrement(5);
slider.setPageIncrement(10);
slider.setSelection(25);
griddata=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata.horizontalSpan=2;
slider.setLayoutData(griddata);
slider.addSelectionListener(
new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
System.out.println("Slider Selection:"+
slider.getSelection());
}
}
);
//
final Scale scale =
new Scale(composite,SWT.HORIZONTAL);
scale.setMinimum(0);
scale.setMaximum(100);
scale.setIncrement(5);
scale.setPageIncrement(5);
scale.setSelection(25);
griddata=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata.horizontalSpan=3;
scale.setLayoutData(griddata);
scale.addSelectionListener(
new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
System.out.println("Scale Selection:"+
scale.getSelection());
}
}
);
Group grp=new Group(composite,SWT.NONE);
grp.setText("test");
gridlayout=new GridLayout(3,false);
grp.setLayout(gridlayout);
data1=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
data1.horizontalSpan=2;
grp.setLayoutData(data1);
Label tl1=new Label(grp,SWT.NULL);
tl1.setText("Test Group");
Text tt2=new Text(grp,SWT.BORDER);
data1=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
data1.horizontalSpan=2;
tt2.setLayoutData(data1);
combobox=new Combo(grp,SWT.BORDER);
combobox.add("First");
combobox.add("Second");
combobox.add("Third");
combobox.add("Fourth");
combobox.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
// combobox
String str=combobox.getText();
System.out.println("the selected value is"+str);
}
});
list=new List(grp,SWT.BORDER|SWT.SINGLE|SWT.V_SCROLL);
String listItems[]={"1","2","3","4","5","6","7","8","9","10","11","12"};
list.setItems(listItems);
griddata=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata.horizontalSpan=2;
int listHeight = list.getItemHeight() * 5;
Rectangle trimHeight = list.computeTrim(0, 0, 0, listHeight);
griddata.heightHint = trimHeight.height;
list.setLayoutData(griddata);
list.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
System.out.println("the selected item is"+list.getItem(list.getSelectionIndex()));
}
});
composite.pack();
shell.setSize(200, 250);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
Swt controls are Text, Label,combobox,Slider,Scale,GridLayout,GridData,Composite,Shell...
package pack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Text;
public class SWTSampleTest1 {
static Shell shell;
static Text t1,t2,t3;
static Label l1,l2,lbltest;
public static void main(String[] args) {
Display display = new Display();
shell= new Shell(display);
shell.setText("Testing...");
shell.setLayout(new FillLayout());
final Combo combobox;
final List list;
//ScrolledComposite scrollComposite=new ScrolledComposite(shell,SWT.NONE|SWT.H_SCROLL|SWT.V_SCROLL);
//Composite composite=new Composite(scrollComposite,SWT.NONE);
//scrollComposite.setContent(composite);
ScrolledComposite scomp=new ScrolledComposite(shell,SWT.H_SCROLL|SWT.V_SCROLL|SWT.NONE);
Composite composite=new Composite(scomp,SWT.NONE);
scomp.setContent(composite);
GridLayout gridlayout=new GridLayout();
gridlayout.numColumns=3;
composite.setLayout(gridlayout);
l1=new Label(composite,SWT.NULL|SWT.BOLD);
l1.setText("Name :");
t1=new Text(composite,SWT.BORDER);
GridData griddata=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata.horizontalSpan=2;
//griddata.horizontalIndent=1;
t1.setLayoutData(griddata);
l2=new Label(composite,SWT.NULL|SWT.BOLD);
l2.setText("MobileNo :");
t2=new Text(composite,SWT.BORDER);
GridData griddata1=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata1.horizontalSpan=2;
t2.setLayoutData(griddata1);
lbltest=new Label(composite,SWT.NULL);
lbltest.setText("AGE :");
t3=new Text(composite,SWT.BORDER);
griddata1=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata1.horizontalSpan=2;
t3.setLayoutData(griddata1);
final Button button=new Button(composite,SWT.NORMAL);
button.setText("Test");
GridData data1=new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
data1.horizontalSpan=2;
button.setLayoutData(data1);
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
System.out.println("Test ok ");
MessageBox messageBox =
new MessageBox(shell,SWT.OK|SWT.CLOSE|SWT.ICON_INFORMATION);
messageBox.setText("Information");
messageBox.setMessage("OK.... BYE...... Thank U!!!!");
if(messageBox.open()==SWT.OK){
String str="\n\t Name: "+t1.getText()+"\n\t Mobile no: "+t2.getText()+"\n\tAge: "+t3.getText();
System.out.println(str);
System.out.println("you came out of the shell. Thank U");
System.exit(0);
}
//System.exit(0);
}
});
final Slider slider =
new Slider(composite,SWT.HORIZONTAL);
slider.setMinimum(0);
slider.setMaximum(100);
slider.setIncrement(5);
slider.setPageIncrement(10);
slider.setSelection(25);
griddata=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata.horizontalSpan=2;
slider.setLayoutData(griddata);
slider.addSelectionListener(
new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
System.out.println("Slider Selection:"+
slider.getSelection());
}
}
);
//
final Scale scale =
new Scale(composite,SWT.HORIZONTAL);
scale.setMinimum(0);
scale.setMaximum(100);
scale.setIncrement(5);
scale.setPageIncrement(5);
scale.setSelection(25);
griddata=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata.horizontalSpan=3;
scale.setLayoutData(griddata);
scale.addSelectionListener(
new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
System.out.println("Scale Selection:"+
scale.getSelection());
}
}
);
Group grp=new Group(composite,SWT.NONE);
grp.setText("test");
gridlayout=new GridLayout(3,false);
grp.setLayout(gridlayout);
data1=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
data1.horizontalSpan=2;
grp.setLayoutData(data1);
Label tl1=new Label(grp,SWT.NULL);
tl1.setText("Test Group");
Text tt2=new Text(grp,SWT.BORDER);
data1=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
data1.horizontalSpan=2;
tt2.setLayoutData(data1);
combobox=new Combo(grp,SWT.BORDER);
combobox.add("First");
combobox.add("Second");
combobox.add("Third");
combobox.add("Fourth");
combobox.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
// combobox
String str=combobox.getText();
System.out.println("the selected value is"+str);
}
});
list=new List(grp,SWT.BORDER|SWT.SINGLE|SWT.V_SCROLL);
String listItems[]={"1","2","3","4","5","6","7","8","9","10","11","12"};
list.setItems(listItems);
griddata=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
griddata.horizontalSpan=2;
int listHeight = list.getItemHeight() * 5;
Rectangle trimHeight = list.computeTrim(0, 0, 0, listHeight);
griddata.heightHint = trimHeight.height;
list.setLayoutData(griddata);
list.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
System.out.println("the selected item is"+list.getItem(list.getSelectionIndex()));
}
});
composite.pack();
shell.setSize(200, 250);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
Comments