Java/SWT JFace Eclipse/Tab — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:57, 1 июня 2010
Содержание
- 1 Create a CTabFolder with min and max buttons, as well as close button and
- 2 Creates a tabbed display with a single tab
- 3 Creates a tabbed display with four tabs and a few controls
- 4 Demonstrates CTabFolder
- 5 implement tab traversal (behave like a tab group)
- 6 Prevent an item from closing
- 7 Prevent Tab from traversing out of a control
- 8 Set the tab traversal order of children
- 9 SWT Tab Control
- 10 Tabbed Example
- 11 TabFolder Example
Create a CTabFolder with min and max buttons, as well as close button and
/*
* Create a CTabFolder with min and max buttons, as well as close button and
* image only on selected tab.
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
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.Text;
public class Snippet165 {
public static void main(String[] args) {
Display display = new Display();
Image image = new Image(display, 16, 16);
GC gc = new GC(image);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
gc.fillRectangle(0, 0, 16, 16);
gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
gc.fillRectangle(3, 3, 10, 10);
gc.dispose();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
folder.setSimple(false);
folder.setUnselectedImageVisible(false);
folder.setUnselectedCloseVisible(false);
for (int i = 0; i < 8; i++) {
CTabItem item = new CTabItem(folder, SWT.CLOSE);
item.setText("Item " + i);
item.setImage(image);
Text text = new Text(folder, SWT.MULTI | SWT.V_SCROLL
| SWT.H_SCROLL);
text.setText("Text for item " + i
+ "\n\none, two, three\n\nabcdefghijklmnop");
item.setControl(text);
}
folder.setMinimizeVisible(true);
folder.setMaximizeVisible(true);
folder.addCTabFolder2Listener(new CTabFolder2Adapter() {
public void minimize(CTabFolderEvent event) {
folder.setMinimized(true);
shell.layout(true);
}
public void maximize(CTabFolderEvent event) {
folder.setMaximized(true);
folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
true));
shell.layout(true);
}
public void restore(CTabFolderEvent event) {
folder.setMinimized(false);
folder.setMaximized(false);
folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
false));
shell.layout(true);
}
});
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
image.dispose();
display.dispose();
}
}
Creates a tabbed display with a single tab
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
/**
* Creates a tabbed display with a single tab
*/
public class TabSimple {
public static void main(String[] args) {
new TabSimple().run();
}
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Simple Tabs");
TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Creates a tabbed display with four tabs and a few controls
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import java.io.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* Creates a tabbed display with four tabs, and a few controls on each page
*/
public class TabComplex {
private static final String IMAGE_PATH = "images"
+ System.getProperty("file.separator");
private Image circle;
private Image square;
private Image triangle;
private Image star;
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Complex Tabs");
createImages(shell);
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the contents
*
* @param shell the parent shell
*/
private void createContents(Shell shell) {
// Create the containing tab folder
final TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
// Create each tab and set its text, tool tip text,
// image, and control
TabItem one = new TabItem(tabFolder, SWT.NONE);
one.setText("one");
one.setToolTipText("This is tab one");
one.setImage(circle);
one.setControl(getTabOneControl(tabFolder));
TabItem two = new TabItem(tabFolder, SWT.NONE);
two.setText("two");
two.setToolTipText("This is tab two");
two.setImage(square);
two.setControl(getTabTwoControl(tabFolder));
TabItem three = new TabItem(tabFolder, SWT.NONE);
three.setText("three");
three.setToolTipText("This is tab three");
three.setImage(triangle);
three.setControl(getTabThreeControl(tabFolder));
TabItem four = new TabItem(tabFolder, SWT.NONE);
four.setText("four");
four.setToolTipText("This is tab four");
four.setImage(star);
// Select the third tab (index is zero-based)
tabFolder.setSelection(2);
// Add an event listener to write the selected tab to stdout
tabFolder.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent event) {
System.out.println(tabFolder.getSelection()[0].getText() + " selected");
}
});
}
/**
* Creates the images
*
* @param shell the parent shell
*/
private void createImages(Shell shell) {
try {
circle = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
+ "circle.gif"));
square = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
+ "square.gif"));
star = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
+ "star.gif"));
triangle = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
+ "triangle.gif"));
} catch (IOException e) {
// Images not found; handle gracefully
}
}
/**
* Disposes the images
*/
private void disposeImages() {
if (circle != null)
circle.dispose();
if (square != null)
square.dispose();
if (star != null)
star.dispose();
if (triangle != null)
triangle.dispose();
}
/**
* Gets the control for tab one
*
* @param tabFolder the parent tab folder
* @return Control
*/
private Control getTabOneControl(TabFolder tabFolder) {
// Create a composite and add four buttons to it
Composite composite = new Composite(tabFolder, SWT.NONE);
composite.setLayout(new FillLayout(SWT.VERTICAL));
new Button(composite, SWT.PUSH).setText("Button one");
new Button(composite, SWT.PUSH).setText("Button two");
new Button(composite, SWT.PUSH).setText("Button three");
new Button(composite, SWT.PUSH).setText("Button four");
return composite;
}
/**
* Gets the control for tab two
*
* @param tabFolder the parent tab folder
* @return Control
*/
private Control getTabTwoControl(TabFolder tabFolder) {
// Create a multi-line text field
return new Text(tabFolder, SWT.BORDER | SWT.MULTI | SWT.WRAP);
}
/**
* Gets the control for tab three
*
* @param tabFolder the parent tab folder
* @return Control
*/
private Control getTabThreeControl(TabFolder tabFolder) {
// Create some labels and text fields
Composite composite = new Composite(tabFolder, SWT.NONE);
composite.setLayout(new RowLayout());
new Label(composite, SWT.LEFT).setText("Label One:");
new Text(composite, SWT.BORDER);
new Label(composite, SWT.RIGHT).setText("Label Two:");
new Text(composite, SWT.BORDER);
return composite;
}
/**
* The entry point for the application
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new TabComplex().run();
}
}
Demonstrates CTabFolder
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates CTabFolder
*/
public class ShowCTabFolder {
// Since CTabFolder does not have a getInsertMark() method,
// store the value so we can keep track of it
private int insertMark = -1;
private CTabFolder tabFolder;
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Show CTabFolder");
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the window"s contents
*
* @param shell the parent shell
*/
private void createContents(Shell shell) {
shell.setLayout(new GridLayout(1, true));
// Create the buttons to create tabs
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
composite.setLayout(new RowLayout());
createButtons(composite);
// Create the tabs
tabFolder = new CTabFolder(shell, SWT.TOP);
tabFolder.setBorderVisible(true);
tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
Display display = shell.getDisplay();
// Set up a gradient background for the selected tab
tabFolder.setSelectionBackground(new Color[] {
display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW),
display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW),
display.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW)}, new int[] { 50,
100});
// Add a listener to get the close button on each tab
tabFolder.addCTabFolderListener(new CTabFolderAdapter() {
public void itemClosed(CTabFolderEvent event) {}
});
}
/**
* Creates the buttons for moving the insert mark and adding a tab
*
* @param composite the parent composite
*/
private void createButtons(Composite composite) {
// Move mark left
Button button = new Button(composite, SWT.PUSH);
button.setText("<<");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (insertMark > -1) {
--insertMark;
resetInsertMark();
}
}
});
// Move mark right
button = new Button(composite, SWT.PUSH);
button.setText(">>");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (insertMark < tabFolder.getItemCount() - 1) {
++insertMark;
resetInsertMark();
}
}
});
// Add a tab
button = new Button(composite, SWT.PUSH);
button.setText("Add Tab");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
new CTabItem(tabFolder, SWT.NONE, insertMark + 1).setText("Tab ("
+ (insertMark + 1) + ")");
}
});
}
/**
* Moves the insert mark to the new location
*/
private void resetInsertMark() {
tabFolder.setInsertMark(insertMark, true);
// Workaround for bug #32846
if (insertMark == -1) {
tabFolder.redraw();
}
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new ShowCTabFolder().run();
}
}
implement tab traversal (behave like a tab group)
/*
* Canvas example snippet: implement tab traversal (behave like a tab group)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet21 {
public static void main(String[] args) {
Display display = new Display();
final Color red = display.getSystemColor(SWT.COLOR_RED);
final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
Shell shell = new Shell(display);
Button b = new Button(shell, SWT.PUSH);
b.setBounds(10, 10, 100, 32);
b.setText("Button");
shell.setDefaultButton(b);
final Canvas c = new Canvas(shell, SWT.BORDER);
c.setBounds(10, 50, 100, 32);
c.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event e) {
switch (e.detail) {
/* Do tab group traversal */
case SWT.TRAVERSE_ESCAPE:
case SWT.TRAVERSE_RETURN:
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS:
case SWT.TRAVERSE_PAGE_NEXT:
case SWT.TRAVERSE_PAGE_PREVIOUS:
e.doit = true;
break;
}
}
});
c.addListener(SWT.FocusIn, new Listener() {
public void handleEvent(Event e) {
c.setBackground(red);
}
});
c.addListener(SWT.FocusOut, new Listener() {
public void handleEvent(Event e) {
c.setBackground(blue);
}
});
c.addListener(SWT.KeyDown, new Listener() {
public void handleEvent(Event e) {
System.out.println("KEY");
for (int i = 0; i < 64; i++) {
Color c1 = red, c2 = blue;
if (c.isFocusControl()) {
c1 = blue;
c2 = red;
}
c.setBackground(c1);
c.update();
c.setBackground(c2);
}
}
});
Text t = new Text(shell, SWT.SINGLE | SWT.BORDER);
t.setBounds(10, 85, 100, 32);
Text r = new Text(shell, SWT.MULTI | SWT.BORDER);
r.setBounds(10, 120, 100, 32);
c.setFocus();
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Prevent an item from closing
/*
* CTabFolder example snippet: prevent an item from closing
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet82 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
for (int i = 0; i < 4; i++) {
CTabItem item = new CTabItem(folder, SWT.CLOSE);
item.setText("Item " + i);
Text text = new Text(folder, SWT.MULTI);
text.setText("Content for Item " + i);
item.setControl(text);
}
final CTabItem specialItem = new CTabItem(folder, SWT.CLOSE);
specialItem.setText("Don"t Close Me");
Text text = new Text(folder, SWT.MULTI);
text.setText("This tab can never be closed");
specialItem.setControl(text);
folder.addCTabFolder2Listener(new CTabFolder2Adapter() {
public void close(CTabFolderEvent event) {
if (event.item.equals(specialItem)) {
event.doit = false;
}
}
});
final CTabItem noCloseItem = new CTabItem(folder, SWT.NONE);
noCloseItem.setText("No Close Button");
Text text2 = new Text(folder, SWT.MULTI);
text2.setText("This tab does not have a close button");
noCloseItem.setControl(text2);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Prevent Tab from traversing out of a control
/*
* Control example snippet: prevent Tab from traversing out of a control
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet127 {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
Button button1 = new Button(shell, SWT.PUSH);
button1.setBounds(10,10,80,30);
button1.setText("no traverse");
button1.addTraverseListener(new TraverseListener () {
public void keyTraversed(TraverseEvent e) {
switch (e.detail) {
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS: {
e.doit = false;
}
}
}
});
Button button2 = new Button (shell, SWT.PUSH);
button2.setBounds(100,10,80,30);
button2.setText("can traverse");
shell.pack ();
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Set the tab traversal order of children
/*
* Composite example snippet: set the tab traversal order of children
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Snippet75 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Composite c1 = new Composite(shell, SWT.BORDER);
c1.setLayout(new RowLayout());
Button b1 = new Button(c1, SWT.PUSH);
b1.setText("B1");
Button[] radios = new Button[3];
for (int i = 0; i < radios.length; i++) {
radios[i] = new Button(c1, SWT.RADIO);
radios[i].setText("R" + (i == 1 ? "&" : "") + i);
}
Button b2 = new Button(c1, SWT.PUSH);
b2.setText("B2");
List l1 = new List(c1, SWT.SINGLE | SWT.BORDER);
l1.setItems(new String[] { "L1" });
Button b3 = new Button(c1, SWT.PUSH);
b3.setText("B&3");
Button b3_1 = new Button(c1, SWT.PUSH);
b3_1.setText("B3_1");
Composite c2 = new Composite(shell, SWT.BORDER);
c2.setLayout(new RowLayout());
Button b4 = new Button(c2, SWT.PUSH);
b4.setText("B&4");
Button b5 = new Button(c2, SWT.PUSH);
b5.setText("B&5");
List l2 = new List(shell, SWT.SINGLE | SWT.BORDER);
l2.setItems(new String[] { "L2" });
ToolBar tb1 = new ToolBar(shell, SWT.FLAT | SWT.BORDER);
ToolItem i1 = new ToolItem(tb1, SWT.RADIO);
i1.setText("I1");
ToolItem i2 = new ToolItem(tb1, SWT.RADIO);
i2.setText("I&2");
Combo combo1 = new Combo(tb1, SWT.READ_ONLY | SWT.BORDER);
combo1.setItems(new String[] { "C1" });
combo1.setText("C1");
combo1.pack();
ToolItem i3 = new ToolItem(tb1, SWT.SEPARATOR);
i3.setWidth(combo1.getSize().x);
i3.setControl(combo1);
i3.setText("I3");
ToolItem i4 = new ToolItem(tb1, SWT.PUSH);
i4.setText("I4");
ToolItem i5 = new ToolItem(tb1, SWT.CHECK);
i5.setText("I5");
Button b6 = new Button(shell, SWT.PUSH);
b6.setText("B&6");
Composite c4 = new Composite(shell, SWT.BORDER);
c4.setSize(32, 32);
Composite c5 = new Composite(c4, SWT.BORDER);
c5.setSize(20, 20);
Control[] list1 = new Control[] { b2, b1, b3_1, b3 };
c1.setTabList(list1);
Control[] list2 = new Control[] { c1, b6, tb1, c4, c2, l2 };
shell.setTabList(list2);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
SWT Tab Control
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
public class TabClass {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Tab Folder Example");
shell.setSize(450, 250);
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
for (int loopIndex = 0; loopIndex < 10; loopIndex++) {
TabItem tabItem = new TabItem(tabFolder, SWT.NULL);
tabItem.setText("Tab " + loopIndex);
Text text = new Text(tabFolder, SWT.BORDER);
text.setText("This is page " + loopIndex);
tabItem.setControl(text);
}
tabFolder.setSize(400, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Tabbed Example
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
public class TabbedShellExample {
Display d;
Shell s;
TabbedShellExample() {
d = new Display();
s = new Shell(d);
s.setSize(250, 200);
s.setText("A Tabbed Shell Example");
s.setLayout(new FillLayout());
TabFolder tf = new TabFolder(s, SWT.BORDER);
TabItem ti1 = new TabItem(tf, SWT.BORDER);
ti1.setText("Option Group");
ti1.setControl(new GroupExample(tf, SWT.SHADOW_ETCHED_IN));
TabItem ti2 = new TabItem(tf, SWT.BORDER);
ti2.setText("Grid");
ti2.setControl(new GridComposite(tf));
TabItem ti3 = new TabItem(tf, SWT.BORDER);
ti3.setText("Text");
Composite c1 = new Composite(tf, SWT.BORDER);
c1.setLayout(new FillLayout());
Text t = new Text(c1, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
ti3.setControl(c1);
TabItem ti4 = new TabItem(tf, SWT.BORDER);
ti4.setText("Settings");
Composite c2 = new Composite(tf, SWT.BORDER);
c2.setLayout(new RowLayout());
Text t2 = new Text(c2, SWT.BORDER | SWT.SINGLE | SWT.WRAP
| SWT.V_SCROLL);
Button b = new Button(c2, SWT.PUSH | SWT.BORDER);
b.setText("Save");
ti4.setControl(c2);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
}
class GroupExample extends Composite {
final Button b1;
final Button b2;
final Button b3;
public GroupExample(Composite c, int style) {
super(c, SWT.NO_BACKGROUND);
this.setSize(110, 75);
this.setLayout(new FillLayout());
final Group g = new Group(this, style);
g.setSize(110, 75);
g.setText("Options Group");
b1 = new Button(g, SWT.RADIO);
b1.setBounds(10, 20, 75, 15);
b1.setText("Option One");
b2 = new Button(g, SWT.RADIO);
b2.setBounds(10, 35, 75, 15);
b2.setText("Option Two");
b3 = new Button(g, SWT.RADIO);
b3.setBounds(10, 50, 80, 15);
b3.setText("Option Three");
}
public String getSelected() {
if (b1.getSelection())
return "Option One";
if (b2.getSelection())
return "Option Two";
if (b3.getSelection())
return "Option Three";
return "None Selected";
}
}
class GridComposite extends Composite {
public GridComposite(Composite c) {
super(c, SWT.BORDER);
GridLayout gl = new GridLayout();
gl.numColumns = 3;
this.setLayout(gl);
final Label l1 = new Label(this, SWT.BORDER);
l1.setText("Column One");
final Label l2 = new Label(this, SWT.BORDER);
l2.setText("Column Two");
final Label l3 = new Label(this, SWT.BORDER);
l3.setText("Column Three");
final Text t1 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t2 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t3 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t4 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t5 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t6 = new Text(this, SWT.SINGLE | SWT.BORDER);
GridData gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l1.setLayoutData(gd);
gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l2.setLayoutData(gd);
gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l3.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t1.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t2.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t3.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t4.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t5.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t6.setLayoutData(gd);
}
public static void main(String[] argv) {
new TabbedShellExample();
}
}
TabFolder Example
/******************************************************************************
* All Right Reserved.
* Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on 2004-4-9 14:11:34 by JACK
* $Id$
*
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
public class TabFolderExample {
Display display = new Display();
Shell shell = new Shell(display);
Image icon = new Image(shell.getDisplay(), "jexp.gif");
public TabFolderExample() {
shell.setLayout(new FillLayout());
final TabFolder tabFolder = new TabFolder(shell, SWT.BOTTOM);
Button button = new Button(tabFolder, SWT.NULL);
button.setText("This is a button.");
TabItem tabItem1 = new TabItem(tabFolder, SWT.NULL);
tabItem1.setText("item #1");
tabItem1.setImage(icon);
tabItem1.setControl(button);
Text text = new Text(tabFolder, SWT.MULTI);
text.setText("This is a text control.");
TabItem tabItem2 = new TabItem(tabFolder, SWT.NULL);
tabItem2.setText("item #2");
tabItem2.setImage(icon);
tabItem2.setControl(text);
Label label = new Label(tabFolder, SWT.NULL);
label.setText("This is a text lable.");
TabItem tabItem3 = new TabItem(tabFolder, SWT.NULL);
tabItem3.setText("item #3");
tabItem3.setControl(label);
tabFolder.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Selected item index = " + tabFolder.getSelectionIndex());
System.out.println("Selected item = " + (tabFolder.getSelection() == null ? "null" : tabFolder.getSelection()[0].toString()));
}
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}
});
//tabFolder.setSelection(new TabItem[]{tabItem2, tabItem3});
//tabFolder.setSelection(2);
shell.setSize(400, 120);
shell.open();
//textUser.forceFocus();
System.out.println(tabFolder.getSelectionIndex());
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new TabFolderExample();
}
}