The target audience must be familiar with Java development and have some experience with eclipse development.
There is a related article on the Eclipse Web site, "Creating Your Own Widgets using SWT," which describes many of the basic concepts and methods of developing your own controls, and is very well described by examples. But its use of the case is relatively simple, there are many control development in the content to be involved, such as keyboard, mouse event processing, scroll bar, focus of processing, and so did not mention. This article provides a comprehensive introduction to the techniques involved in customizing controls by developing an instance of a custom color list control. At the same time, the reader can also extend the instance to implement its own list control.
The standard list control provided in SWT is very simple and only provides a choice of strings. We often need to provide some graphical lists for users to choose from, which requires them to develop their own custom list controls. The color selection list is one of our most popular graphic lists, and we'll take that as an example. Here's a list of the colors we're going to develop.
We mainly consider the following issues when developing custom controls:
1. Custom control drawing: Usually we need to draw the shape or pattern of the control.
2, the control of the keyboard event response: When the focus into the control, the user to the keyboard, the control through the keyboard control, we need to let the control of the user's actions to respond. For example, in the list, the user will change the list selection by the up and down arrows;
3, the control of the mouse event response: When the user with the mouse to select the control, to operate, the control must react accordingly;
4, control response to focus events: When the interface focus enters or moves out of the control, we usually need to draw the control to the shape that gets or loses focus. For example, when the focus enters the list, the list item that is normally selected will have a dashed frame to indicate that it is selected.
5. Response tab: For a control that can be manipulated, the user can use the TAB key to move the focus into or out.
6, response to the scroll bar event: When the control has a scroll bar, we need to respond to the user to the scroll bar operation, the control to complete the drawing work.
7. Provide event monitoring mechanism: programmers use your controls often need to listen to some of the events in the control, so that when the event occurs, they can handle accordingly.
8, the provision of accessibility (accessibility): Accessibility is necessary to facilitate the use of people with disabilities, standard controls will provide appropriate support, our custom controls are no exception.
9, provide functional interface to facilitate programmer access: usually for the convenience of programmers to get information in the control or to set up, we need to provide some interface.
First of all, the list control we are developing is a basic control, so we choose canvas as the base class for our development.
public class ColorList extends Canvas {
Vector colors = new Vector(); // 用于保存我们颜色控件中的颜色值
Vector colorNames = new Vector(); // 用于保存颜色控件中的颜色名字
int rowSel = -1; // 用于保存当前选中的行号
int oldRowSel = -1; // 用于保存上一次选中的行号
int maxX, maxY; // 用于保存列表的宽度和高度
int lineHeight; // 用于设置行高
int cx = 0; // 滚动条滚动后,控件的图形相对于控件可见区域左上角的x坐标
int cy = 0; // 滚动条滚动后,控件的图形相对于控件可见区域左上角的y坐标
}