|
|
< Day Day Up > |
|
9.3 Classes TabFolder and TabItem9.3.1 Example
9.3.2 TabFolder and TabItem Hierarchies
9.3.3 TabFolder Styles
9.3.4 TabFolder Events
9.3.5 TabItem Styles (none)
9.3.6 TabItem Events (none)
Sometimes called notebooks, tab folders provide a very simple "pages-in-a-book" user interface metaphor. Tabs in the book, represented by instances of the class TabItem, provide random access to the pages. When the user selects a tab item, the item comes to the front, and the client area of the tab folder is filled with the controls that represent the page. Tab items can contain text, icons, or both. Tab folders operate in one of two paging modes: automatic and manual. In automatic mode, the application program creates each page when the tab folder is created. When the user selects a tab, pages are shown automatically. In manual mode, the application program listens for selection events on the tab folder and provides the contents of each page on demand. Most programs do not use manual mode because it is somewhat more complicated to use than automatic mode. Manual mode provides the most benefit when pages are expensive to create, allowing you to delay the creation of pages until they are needed. The following example program creates a tab folder in automatic mode, filling each page with a push button. Figure 9.6 shows the result.
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
TabFolder folder = new TabFolder(shell, SWT.NONE);
for (int i = 0; i < 4; i++) {
TabItem item = new TabItem(folder, SWT.NONE);
item.setText("Item " + i);
Button button = new Button(folder, SWT.PUSH);
button.setText("Button " + i);
item.setControl(button);
}
folder.setSize(400, 400);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
Figure 9.6.
Tab folders are often used in dialogs that allow the user to set program preferences. Before the advent of the tab folder, users often had to navigate several levels of dialogs to set all the preferences for a particular area. This was cumbersome. Sometimes when navigating through the chain of dialogs, users would forget from which dialog they had come and the preferences they were trying to change. The nice thing about tab folders is that they are compact and allow fast access to any of the pages. The tabs themselves also give a quick summary of the operations that are available.[10]
9.3.7 Text and ImagesTabItem supports the standard API for text and images. TabItem supports mnemonics but does not support wrapping or more than one line of text in an item.
9.3.8 Tool TipsTabItem supports tool tips in a manner that is similar to ToolItem.
9.3.9 Automatic PagingAutomatic paging is implemented for tab folders using page controls. A page control is the control that will fill the client area of a tab folder when the user selects a tab. The previous page control, belonging to another tab, is automatically hidden after the new page has been displayed. Page controls are properties of tab items, even though the control itself must be a child of the tab folder. This can be a little confusing at first. Because tab items are not controls, the page control cannot be a child of a tab item, as you might expect.[11]
Page controls are assigned using setControl().
9.3.10 The SelectionApplication programs sometimes need to manipulate the selection in a tab folder, either querying it or setting it. For example, some programs remember the last tab item that was selected in a dialog that contains a tab folder and restore the selection to that tab item when the user reopens the dialog. Tab folders are index-based controls, so the selection can be specified in terms of indices. Tab folders are always single-select. There can never be more than one tab item selected at a time. Despite this fact, to be consistent with other controls that contain items, methods that get and set the selection in terms of items use arrays.[12]
The following methods are used to manipulate the selection in a tab folder.
9.3.11 Searching OperationsTabFolder provides a number of index and locating operations that allow an application program to find items and indices.
9.3.12 TabFolder EventsSWT.Selection (SelectionEvent)The SWT.Selection event (typed event SelectionEvent) is sent whenever the user selects an item with the mouse or the keyboard. The relevant event fields during a selection event for TabItem are as follows.
Using SWT.Selection to Implement Manual PagingUnder certain circumstances, you may wish to implement manual paging in a tab folder. For example, it may be too expensive to create all of the page controls up front. Sometimes the pages are mostly the same between tabs. Manual paging allows you to reuse controls between pages by changing the contents of the client area when the user selects a tab, instead of hiding and showing a single page control. The following example implements a manual and automatic paging hybrid. Pages are created lazily. Once a page has been created, it becomes automatically displayed the next time the user selects the tab.
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final TabFolder folder = new TabFolder(shell,SWT.NONE);
folder.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
TabItem item = (TabItem) event.item;
if (item.getControl() != null) return;
Button button = new Button(folder, SWT.PUSH);
button.setText("Button "+folder.indexOf(item));
item.setControl(button);
}
});
for (int i = 0; i < 4; i++) {
TabItem item = new TabItem(folder, SWT.NONE);
item.setText("Item " + i);
}
folder.setSize(400, 400);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
|
|
|
< Day Day Up > |
|