A
Composite widget can contain other widgets, the way how you are adding widgets
to shell, you can add widgets to Composite.
How to define a
Composite?
Composite
composite1 = new Composite(shell,SWT.BORDER);
How to add widgets to
a composite?
Just
like how you are adding widgets to Shell, you can add widgets to composite
also.
Label
label = new Label(composite1,SWT.NONE);
The
position of widgets inside the composite are relative to the composite. You can
apply following styles to Composite widget.
Widget
|
Description
|
NO_BACKGROUND
|
Style
constant for no background behavior
|
NO_FOCUS
|
Style
constant for no focus from the mouse behavior
|
NO_MERGE_PAINTS
|
Style
constant for no paint event merging behavior
|
NO_REDRAW_RESIZE
|
Style
constant for no redraw on resize behavior
|
NO_RADIO_GROUP
|
Style
constant for preventing child radio group behavior
|
EMBEDDED
|
Style
constant to allow embedding
|
DOUBLE_BUFFERED
|
Style
constant to indicate double buffering
|
package swt_app; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; 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.Shell; public class Test { private static int xPosition = 30; private static int yPosition = 30; private static int shellWidth = 600; private static int shellHeight = 600; private static int compositeWidth = 250; private static int compositeHeight = 250; private static String[] countries = { "India", "Germany", "Canada", "Japan", "Sri Lanka", "Singpore", "Russia" }; private static void addWidgetsToShell(Display display, Shell shell) { Composite composite = new Composite(shell, SWT.BORDER); composite.setBackground(new Color(display, 102, 153, 153)); composite.setBounds(10, 10, compositeWidth, compositeHeight); Combo combo = new Combo(composite, SWT.DROP_DOWN); combo.setItems(countries); combo.setBounds(xPosition, 20, 150, 100); combo.setText("Select your country"); yPosition += 100; Button button1 = new Button(composite, SWT.PUSH); button1.setText("Submit"); button1.setLocation(xPosition + 100, yPosition); button1.setSize(100, 20); button1.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println("Country Selected is : " + countries[combo.getSelectionIndex()]); } }); } public static void main(String[] args) { /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell * to Display */ Shell shell = new Shell(display); shell.setSize(shellWidth, shellHeight); addWidgetsToShell(display, shell); /* Open shell window */ shell.open(); /* * Run the event dispatching loop until an exit condition occurs, which * is typically when the main shell window is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } /* Dispose the display */ display.dispose(); } }
Select
any country and submit, you can able to see the selected country in the console.
No comments:
Post a Comment