|
|
< Day Day Up > |
|
10.3 Classes ScrollBar and Slider10.3.1 Example
10.3.2 ScrollBar and Slider Hierarchy
10.3.3 ScrollBar and Slider Styles
10.3.4 ScrollBar and Slider Events
ScrollBar and Slider are analogous to Scale. Like Scale, they allow the user to select from a continuous series of integer values. Unlike Scale, they provide a programmable thumb that is often used to show the proportion of the visible area of a document, compared with the total area needed to display it. ScrollBar and Slider are range-based classes, allowing the user to select a value between the minimum and maximum, less the size of the thumb. Both controls draw in a similar manner, have the same API, and are manipulated in the same manner by the user. Instances of the class Slider are controls, whereas instances of ScrollBar are not. Sliders are created and manipulated like any other control. Scroll bars, on the other hand, are part of the trim, normally appearing to the right and bottom of a control, outside the client area. When the control is resized, the scroll bars are moved and resized with the rest of the trim.
The following example code creates a horizontal and a vertical scroll bar and a horizontal slider in a shell.
public static void main(String[] args) {
Display display = new Display();
int style = SWT.SHELL_TRIM|SWT.H_SCROLL|SWT.V_SCROLL;
Shell shell = new Shell(display, style);
Slider slider = new Slider(shell, SWT.H_SCROLL);
slider.pack();
slider.setLocation(20, 20);
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
10.3.5 Scroll BarsScroll bars are created when their parent is created. Unlike other widgets in SWT, the SWT.H_SCROLL and SWT.V_SCROLL styles are given to the parent, causing the parent to create the scroll bar. We have already seen these styles used to indicate horizontal and vertical scroll bars in the classes Text, List, Tree, and Table. Once the scroll bar has been created, it is queried from its parent.[2] You cannot create a scroll bar using a ScrollBar constructor.
10.3.6 Range OperationsScrollBar and Slider provide the following range operations.
10.3.7 The Selection and the ThumbThe selection refers to the position of the thumb, which is the current integer value in a scroll bar or a slider. The thumb, unlike the thumb portion of a scale, has a size that affects the available range, which is defined as minimum to maximum minus the thumb size.
10.3.8 Increment and Page IncrementJust like Scale, Slider and ScrollBar support increment and page increments. The API works in exactly the same manner.
10.3.9 Configuring OperationsScrollBar and Slider perform range checks when setting the selection, thumb, minimum, maximum, increment, and page increment. Sometimes the order in which these properties are set determines the success of the operation. For example, if the minimum is zero and the maximum is 100, this code fails. //fails when minimum is 0 and maximum is 100 slider.setMinimum(120); slider.setMaximum(140); This code, on the other hand, works. //works when minimum is 0 and maximum is 100 slider.setMaximum(140); slider.setMinimum(120); Setting the maximum before the minimum does not work every time either. For example, when the minimum is 120, the maximum is 140, and the desired minimum and maximum are to be zero and 100, the minimum must be set before the maximum or the code will fail. The setValues() method avoids problems of this kind and can be more efficient, depending on the platform.
10.3.10 ScrollBar and Slider EventsSWT.Selection (SelectionEvent)The SWT.Selection event (typed event SelectionEvent) is sent whenever the user changes the value of a scroll bar or a slider using the mouse or the keyboard. The relevant event fields during a selection event for ScrollBar or Slider are as follows.
Using SWT.Selection to Scroll a List of StringsThe following example implements a very simple scrolling list of strings. It shows you how to create a control with scroll bars and listen for SWT.Selection. First, a shell is created with both horizontal and vertical scroll bars. We make use of the fact that Shell is a subclass of Composite and can have scroll bars. The SWT.NO_BACKGROUND and SWT.NO_REDRAW_RESIZE style bits are used to minimize flashing when the shell is painted and resized. Next, an SWT.Selection listener is created and added to each scroll bar. This listener redraws the entire list when the scroll bar is selected. A more efficient scrolling implementation would probably use Canvas.scroll() to copy and damage the client area. An SWT.Paint listener uses the current position of each scroll bar to draw the strings. The horizontal scroll bar scrolls in pixel units. The x coordinate of each visible item in the list is the current location of the horizontal scroll bar (negated). The vertical scroll bar scrolls the list in index units. The y coordinate of each visible item is computed using the current location of ScrollBar to get the first visible index. Note that the SWT.Paint listener uses gc.fillRectangle() to ensure that every pixel is drawn, a requirement of SWT.NO_BACKGROUND. To configure the horizontal scroll bar, an SWT.Resize listener computes the pixel width of the widest item and uses this value as the horizontal scroll bar maximum. The thumb is the minimum of the client area and the widest item. This gives a range from zero to the widest item, less the client area width, which is exactly the amount that needs to be scrolled to show the widest item. The vertical scroll bar maximum is the number of items in the list. The thumb is the number of completely visible items, which is computed by dividing the height of the client area by the height of an item. The page increment is assigned to be one less than the thumb, causing <Page Down> and <Page Up> always to show one item from the previous page. Because of the SWT.NO_RESIZE_REDRAW style, when the shell becomes larger and the scroll bars are configured, either scroll bar selection can change. This causes the location of the entire list to change, creating a problem because only the exposed areas were damaged. To avoid drawing garbage on the screen, the shell is completely redrawn whenever this happens.
public static void main(String[] args) {
final String[] list = new String[128];
for (int i = 0; i < list.length; i++) {
list[i] = i + "-String-that-is-quite-long-" + i;
}
Display display = new Display();
int style = SWT.SHELL_TRIM|SWT.H_SCROLL|SWT.V_SCROLL;
style |= SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE;
final Shell shell = new Shell(display, style);
Listener scrollListener = new Listener() {
public void handleEvent(Event event) {
shell.redraw();
}
};
final ScrollBar hBar = shell.getHorizontalBar();
final ScrollBar vBar = shell.getVerticalBar();
hBar.addListener(SWT.Selection, scrollListener);
vBar.addListener(SWT.Selection, scrollListener);
final Color listForeground =
display.getSystemColor(SWT.COLOR_LIST_FOREGROUND);
final Color listBackground =
display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
shell.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event event) {
GC gc = event.gc;
gc.setForeground(listForeground);
gc.setBackground(listBackground);
Rectangle rect = shell.getClientArea();
gc.fillRectangle(rect);
int x = -hBar.getSelection();
int height = gc.stringExtent("").y;
int start = vBar.getSelection();
int end =
Math.min(
list.length - 1,
start + rect.height / height);
for (int i = start; i <= end; i++) {
gc.drawText(
list[i],
x + 2,
(i - start) * height + 2);
}
}
});
shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
Rectangle rect = shell.getClientArea();
GC gc = new GC(shell);
int width = 0;
for (int i = 0; i < list.length; i++) {
width =
Math.max(
width,
gc.textExtent(list[i]).x);
}
width += 4;
hBar.setMaximum(width);
hBar.setThumb(Math.min(width, rect.width));
hBar.setPageIncrement(hBar.getThumb());
int height = gc.stringExtent("").y;
vBar.setMaximum(list.length);
int page = Math.max(1, rect.height / height);
vBar.setThumb(Math.min(page, list.length));
vBar.setPageIncrement(vBar.getThumb() - 1);
gc.dispose();
if (hSelection != hBar.getSelection()
|| vSelection != vBar.getSelection()) {
shell.redraw();
}
}
});
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
|
|
|
< Day Day Up > |
|