Õ¾ÄÚËÑË÷: ÇëÊäÈëËÑË÷¹Ø¼ü´Ê
µ±Ç°Ò³Ãæ: ͼÊéÊ×Ò³ > SWT: The Standard Widget Toolkit

9.3 Classes TabFolder and TabItem - SWT: The Standard Widget Toolkit

Previous Section  < Day Day Up >  Next Section

9.3 Classes TabFolder and TabItem

9.3.1 Example

graphics/09inf05.gif

9.3.2 TabFolder and TabItem Hierarchies

graphics/09inf06.gif

9.3.3 TabFolder Styles

Style

Description

SWT.TOP

Items are placed at the top of the tab folder

SWT.BOTTOM

Items are placed at the bottom of the tab folder


9.3.4 TabFolder Events

Event

Description

SWT.Selection

A tab item was selected


9.3.5 TabItem Styles (none)

Style

Description


9.3.6 TabItem Events (none)

Event

Description


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.

graphics/09fig06.gif


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]

[10] Another technique to avoid nested dialogs is to use a tree of options instead of a tab folder. When the user selects an item in the tree, a page is displayed. The advantage of this approach over tab folders is that options can be presented to the user hierarchically.

9.3.7 Text and Images

TabItem 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.

setText(String string) Sets the text for the item. The string cannot contain line delimiters. Mnemonics characters, indicated by '&', are supported.

getText() Returns the item text. This is an empty string if the text has never been set.

setImage(Image image) Sets the image for the item.

The First Image Defines the Size of All Images in the Control

Just like ToolBar, Tree, and Table, TabFolder scales the image it displays to be the size of the first image inserted into the control. Thanks Microsoft!


getImage() Returns the item image. This is null if the image has never been set.

9.3.8 Tool Tips

TabItem supports tool tips in a manner that is similar to ToolItem.

setToolTipText(String string) Sets the tool tip text for the item. This string is displayed when the mouse hovers within the bounds of the item. Setting this string to null removes the tool tip.

getToolTipText() Returns the tool tip text.

9.3.9 Automatic Paging

Automatic 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]

[11] Strong typing prevents this.

Page controls are assigned using setControl().

setControl(Control control) Sets the page control for the tab item. The page control is automatically resized to fit the client area of the tab folder when the tab is selected. The tab folder hides the previous page control that belongs to another tab item.

getControl() Returns the page control. If no control has been set, null is returned.

9.3.10 The Selection

Application 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]

[12] By the time you read this, methods may have been defined that take single items for R3.0.

The following methods are used to manipulate the selection in a tab folder.

getSelection() Returns an array of the selected items. When there are no selected items, the array is empty. Because tab folders are always single-select, this array is either of size zero or size 1.

getSelectionIndex() Returns the index of the item that is selected. If no items are selected, –1 is returned. On some platforms, it is not possible to have a tab folder with no selection. In this case, –1 can never be returned.

setSelection (int index) Sets the selection to the item at the index. If the index is out of range, it is ignored. If the current tab item has a page control, it is hidden before the new tab item and page control are shown.

setSelection(TabItem[ ] items) Sets the selection to items in the array. If the array is empty and the platform allows a tab folder to have no selection, the selection is cleared. Otherwise, the selection is set to the first item in the array. Page controls are hidden and shown in the same manner as setSelection(int).

9.3.11 Searching Operations

TabFolder provides a number of index and locating operations that allow an application program to find items and indices.

getItem(int index) Returns the item at the index that is zero-based. If the index is out of range, an IllegalArgumentException ("Index out of bounds") is thrown.

getItemCount() Returns the number of items in the tab folder.

getItems() Returns an array of the items. This is a copy so that modifying the array has no effect.

indexOf(TabItem item) Returns the zero-based index of the item in the tab folder. If the item is not found, –1 is returned.

9.3.12 TabFolder Events

SWT.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.

Public Fields of Class Event That Are Valid during SWT.Selection

Field

Description

item

The item that was selected


Using SWT.Selection to Implement Manual Paging

Under 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();

}


    Previous Section  < Day Day Up >  Next Section