站内搜索: 请输入搜索关键词
当前页面: 图书首页 > SWT: The Standard Widget Toolkit

11.1 Class Control - SWT: The Standard Widget Toolkit

Previous Section  < Day Day Up >  Next Section

11.1 Class Control

11.1.1 Example (none梒lass is abstract)

11.1.2 Control Hierarchy

graphics/11inf01.gif

11.1.3 Control Styles

Style

Description

SWT.BORDER

Draw the control with the appropriate border

SWT.LEFT_TO_RIGHT

Force the control orientation to be left to right

SWT.RIGHT_TO_LEFT

Force the control orientation to be right to left


11.1.4 Control Events

Event

Description

SWT.DragDetect

The user requested a drag and drop operation

SWT.FocusIn

The control is now the focus control

SWT.FocusOut

The control is no longer the focus control

SWT.Help

The user requested help for the control

SWT.KeyDown

A key was pressed

SWT.KeyUp

A key was released

SWT.MenuDetect

The user requested a context menu

SWT.MouseDoubleClick

A mouse button was pressed twice

SWT.MouseDown

A mouse button was pressed

SWT.MouseEnter

The mouse entered the client area of the control

SWT.MouseExit

The mouse exited the client area of the control

SWT.MouseHover

The mouse lingered over the control

SWT.MouseUp

A mouse button was released

SWT.MouseMove

The mouse was moved

SWT.Move

The position of the control was changed

SWT.Paint

The control was asked to draw

SWT.Resize

The size of the client area of the control changed


Controls are one of the most basic building blocks in SWT. As such, they have already been described in great detail in the chapters Widget Fundamentals, The Keyboard, The Mouse, and Control Fundamentals. Topics that have been covered in those chapters will not be covered again here.

11.1.5 Parent, Shell, and Monitor

You can query the parent of a control, the shell that contains a control, and the monitor that contains a control using getParent(), getShell(), and getMonitor().

getParent() Returns the parent of the control. If the control is a shell, the parent can be either null, indicating a "top-level" window, or another shell, when the control is a "dialog" window.

getShell() Returns the shell that contains the control. If the control is a shell, the control is returned. Use getParent() to get the parent shell of a dialog shell.

getMonitor() Returns the monitor that contains the control. If the control straddles monitors, a platform-specific algorithm is used to determine the "best" monitor, which is the one that contains the majority of the control.

11.1.6 Context Menus

Every control can contain a single context menu that is displayed when the user requests it. The context menu, as described in the section Classes Menu and MenuItem, must be created with the style SWT.POP_UP.

setMenu(Menu menu) Sets the context menu for the control. When the user performs the default action for the platform to request a context menu, this menu is displayed. To override this behavior or compute the context menu for a control dynamically, see Detecting a Context Menu Request in the chapter The Mouse.

getMenu() Returns the context menu for the control. This value is null when no menu has been set into the control.

11.1.7 Foreground, Background, and Font

Controls support the ability to set the foreground and background colors, as well as the font. On some platforms, the theme manager may refuse the color or font change.

setForeground(Color color) Sets the foreground color of the control. If the color is null, the default foreground color for the control is restored.

getForeground() Returns the foreground color of the control. If the foreground color has not been set, the default foreground color for the control is returned.

setBackground(Color color) Sets the background color of the control. If the color is null, the default background color for the control is restored.

getBackground() Returns the background color of the control. If the background color has not been set, the default background color for the control is returned.

setFont(Font font) Sets the font that is used to draw text in the control. If the font is null, the default font for the control is restored.

getFont() Returns the font that is used to draw text in the control. If the font has not been set, the default font for the control is returned.

11.1.8 Tool Tips

Tool tips are short descriptive messages that appear when the mouse lingers within a control. Tool tips are supported for controls using the setToolTipText() method.

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

getToolTipText() Returns the tool tip text. If the tool tip text has never been set, the default value is null, indicating that no tool tip text should be displayed.

Note that the duration, location, and mouse gesture that trigger the tool tip are platform-specific. Tool tips can also be found in the classes ToolItem and TabItem.

11.1.9 Cursors

Setting the cursor for a control is supported using the setCursor() method.

setCursor(Cursor cursor) Sets the cursor for the control. The cursor is displayed immediately if the mouse is inside the control. Otherwise, it is displayed whenever the mouse enters the control. If the cursor is null, the default cursor for the control is restored. For example, a text control might restore the cursor to an I-beam, rather than an arrow.

If a cursor is not defined for a control, it is inherited. This means that setting the cursor for a parent implicitly defines the cursor for the child, provided that the child does not explicitly set its own cursor. When a child is disabled, the cursor is inherited. This makes sense, given that mouse events for a disabled control are delivered to the parent. For example, a disabled text control will not respond to mouse clicks, take focus, or show an I-beam.

Some controls use more than one cursor. For example, a table may show a resize cursor when the mouse is moved over a column. In this case, setCursor() is called from an SWT.MouseMove listener for the table to set the appropriate cursor.

See the Cursors chapter for more on working with cursors.

    Previous Section  < Day Day Up >  Next Section