1
|
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
2
|
|
3
|
<head>
|
4
|
<meta http-equiv="Content-Language" content="en-us">
|
5
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
6
|
</head>
|
7
|
|
8
|
|
9
|
<table>
|
10
|
<tr>
|
11
|
<td bgcolor="#000080" colspan="2"><b><font color="#FFFFFF">Example 3
|
12
|
- Clicking and Scrolling</font></b></td>
|
13
|
</tr>
|
14
|
<tr>
|
15
|
<td valign="top">
|
16
|
<p> </p>
|
17
|
<p>In this example, a Button is used to create CheckBoxes inside a ScrollPane.</p>
|
18
|
<p>The button and scrollpane are placed absolutely inside the contents
|
19
|
figure by simply setting their bounds; no layout manager is used there.
|
20
|
However, the
|
21
|
<u onmouseout="view.style.border ="";view.style.background="#FFFFFF"" onmouseover="view.style.border ="1px solid #000080";view.style.background="#BBBBFF"">
|
22
|
view</u> must have a Layout, or its preferred size will not get calculated,
|
23
|
and scrolling will not work correctly.</p>
|
24
|
<p>The button's action listener will get called each time the user clicks
|
25
|
on the button. The
|
26
|
<u onmouseout="listener.style.border ="";listener.style.background="#FFFFFF"" onmouseover="listener.style.border ="1px solid #000080";listener.style.background="#BBBBFF"">
|
27
|
listener</u> will create a new CheckBox and add it to the view. A vertical scrollbar will appear when the checkboxes cannot all be displayed at once in the pane.</p>
|
28
|
</td>
|
29
|
<td><img border="0" src="demo3.gif" width="263" height="330"></td>
|
30
|
</tr>
|
31
|
</table>
|
32
|
<table>
|
33
|
<tr>
|
34
|
<td valign="top">
|
35
|
<pre>01
|
36
|
02
|
37
|
03
|
38
|
04
|
39
|
05
|
40
|
06
|
41
|
07
|
42
|
08
|
43
|
09
|
44
|
10
|
45
|
11
|
46
|
12
|
47
|
13
|
48
|
14
|
49
|
15
|
50
|
16
|
51
|
17
|
52
|
18
|
53
|
19
|
54
|
20
|
55
|
21
|
56
|
22
|
57
|
23
|
58
|
24
|
59
|
25
|
60
|
26
|
61
|
27
|
62
|
28
|
63
|
29
|
64
|
30
|
65
|
31
|
66
|
32
|
67
|
33
|
68
|
34
|
69
|
35
|
70
|
36
|
71
|
37
|
72
|
38
|
73
|
39
|
74
|
40
|
75
|
41
|
76
|
42
|
77
|
43
|
78
|
44
|
79
|
45
|
80
|
46
|
81
|
47
|
82
|
</pre></td>
|
83
|
<td valign="top">
|
84
|
<pre><font color="#000084">import</font> org.eclipse.swt.widgets.Shell;
|
85
|
<font color="#000084">import</font> org.eclipse.swt.widgets.Display;
|
86
|
<font color="#000084">import</font> org.eclipse.draw2d.*;
|
87
|
<font color="#000084">import</font> org.eclipse.swt.SWT;
|
88
|
<font color="#000084">import</font> org.eclipse.draw2d.geometry.*;
|
89
|
|
90
|
<font color="#000084">public</font> <font color="#000084">class</font> Demo3 {
|
91
|
|
92
|
<font color="#000084">static int</font> count = 1;
|
93
|
|
94
|
<font color="#000084">public static void</font> main(String args[]){
|
95
|
Shell shell = <font color="#000084">new</font> Shell();
|
96
|
shell.setSize(350,350);
|
97
|
shell.open();
|
98
|
shell.setText(<font color="#008484">"Demo 3"</font>);
|
99
|
LightweightSystem lws = <font color="#000084">new</font> LightweightSystem(shell);
|
100
|
IFigure panel = <font color="#000084">new</font> Figure();
|
101
|
lws.setContents(panel);
|
102
|
ScrollPane scrollpane = <font color="#000084">new</font> ScrollPane();
|
103
|
scrollpane.setBounds(<font color="#000084">new</font> Rectangle(30,30,210,200));
|
104
|
scrollpane.getViewport().setBorder(<font color="#000084">new</font> GroupBoxBorder(<font color="#008484">"Viewport"</font>));
|
105
|
scrollpane.setBorder(<font color="#000084">new</font> GroupBoxBorder(<font color="#008484">"ScrollPane"</font>));
|
106
|
<div id="view"> <font color="#000084">final</font> Figure view = <font color="#000084">new</font> Figure();
|
107
|
view.setBorder(<font color="#000084">new</font> GroupBoxBorder(<font color="#008484">"The View"</font>));
|
108
|
view.setLayoutManager(<font color="#000084">new</font> FlowLayout(FlowLayout.VERTICAL));
|
109
|
scrollpane.setView(view);</div>
|
110
|
Clickable button = <font color="#000084">new</font> Button(<font color="#008484">"Create checkbox"</font>);
|
111
|
button.setBounds(<font color="#000084">new</font> Rectangle(30,250,140,35));
|
112
|
<div id="listener"> button.addActionListener(<font color="#000084">new</font> ActionListener(){
|
113
|
<font color="#000084">public void</font> actionPerformed(ActionEvent e){
|
114
|
view.add(<font color="#000084">new</font> CheckBox(<font color="#008484">"Checkbox "</font>+count++));
|
115
|
}
|
116
|
});</div>
|
117
|
panel.add(button);
|
118
|
panel.add(scrollpane);
|
119
|
|
120
|
Display display = Display.getDefault();
|
121
|
<font color="#000084">while</font> (!shell.isDisposed()) {
|
122
|
<font color="#000084">if</font> (!display.readAndDispatch())
|
123
|
display.sleep ();
|
124
|
}
|
125
|
}
|
126
|
}</pre></td></tr></table>
|