addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
}
public void mouseDown(MouseEvent e) {
int row = (ey cy) / lineHeight; //计算选中的行
if (row >= ) {
oldRowSel = rowSel;
rowSel = row;
}
if (oldRowSel != rowSel) { // 重画旧的和新的选择项
((Canvas) egetSource())redraw(cx (ey / lineHeight)
* lineHeight maxX lineHeight false);
((Canvas) egetSource())redraw(cx (oldRowSel + cy
/ lineHeight)
* lineHeight maxX lineHeight false);
}
selectionChanged();
}
public void mouseUp(MouseEvent e) {
}
});
当我们的控件获得焦点时选中的列表项需要有虚框表示控件得到焦点当获得或失去焦点是我们这里只需要简单的通知选中的项重画
addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
((Canvas) egetSource())redraw(cx rowSel * lineHeight maxX
lineHeight true);
}
public void focusLost(FocusEvent e) {
((Canvas) egetSource())redraw(cx rowSel * lineHeight maxX
lineHeight true);
}
});
我们在绘制每一个列表项时可以加入判断当前控件是否得到焦点如果控件得到了焦点我们就在选中的项目上画一个虚框下面是我们绘制一个列表项的代码注意在代码的最后绘制焦点的虚框
void onPaint(GC gc
int row
int beginx
int beginy
boolean isSelected) {
Color initColor = gcgetBackground();
Color initForeColor = gcgetForeground();
if (isSelected) {
gcsetBackground(DisplaygetCurrent()getSystemColor(
SWTCOLOR_LIST_SELECTION));
gcfillRectangle(beginx beginy maxX lineHeight);
gcsetForeground(DisplaygetCurrent()getSystemColor(
SWTCOLOR_LIST_SELECTION_TEXT));
} else {
gcsetBackground(initColor);
}
gcdrawString((String) colorNamesget(row) beginx + beginy);
Color color = DisplaygetCurrent()getSystemColor(
((Integer) colorsget(row))intValue());
gcsetBackground(color);
gcfillRectangle(beginx + beginy + lineHeight );
gcsetBackground(initColor);
gcsetForeground(initForeColor);
if (isFocusControl() && isSelected)
gcdrawFocus(cx beginy maxX lineHeight);
}
[] [] [] [] [] []