我要用SWT做一个Table,可以选择多个单元,现在的问题是点击第一列时总有一个阴影出现,请教各位大虾该如何解决,实行结果的图片贴不上来,辛苦各位大虾了package cn.com.dhc.testExample;import java.util.HashMap;
import java.util.Map;import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;public class TestTable {
private boolean mouseFlg = false;
private boolean moveFlg = false;
int [] beginCell = {-1, -1}; 
int [] startCell = {-1, -1}; 
int [] endCell = {-1, -1}; 
int [] oldCell = {-1, -1}; 
private Color defaultColor = new Color(null, 192, 192, 192);
Table table;
public  void open() {
int style = SWT.NONE;
Shell shell = new Shell(SWT.CLOSE);
table = new Table(shell, SWT.SINGLE);
table.setHeaderVisible(true);
for (int i = 0; i < 5; i++) {
TableColumn column = new TableColumn(table, SWT.CENTER);
column.setText("a" + i);
column.setWidth(80);
}
for (int i = 0; i < 18; i++) {
TableItem item = new TableItem(table, style);
item.setText(new String[] {"a", "b","c","d","e"});
}
table.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseDown(MouseEvent e) {
System.out.println(e.button);
// TODO Auto-generated method stub
if (startCell[0] != -1 && startCell[1] != -1) {
System.out.println("MouseDown:delMoveColor");
delMoveColor(table);
startCell = new int[] {-1, -1}; 
endCell = new int[] {-1, -1}; 
}
mouseFlg = true;
beginCell = getCell(table, e.x, e.y);
System.out.println("startCell:" + startCell[0] + ":" + startCell[1]);
} public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub

endCell = getCell(table, e.x, e.y);
resetArea(beginCell, endCell);
System.out.println("endCell:" + endCell[0] + ":" + endCell[1]);
if (!moveFlg) {
TableItem item = table.getItem(endCell[0]);
item.setBackground(endCell[1], getPlusColor(item.getBackground(endCell[1]), defaultColor));
}
mouseFlg = false;
moveFlg = false;
beginCell = new int[] {-1, -1};  oldCell = new int[] {-1, -1}; 
}

});
table.addMouseMoveListener(new MouseMoveListener() { public void mouseMove(MouseEvent e) {
if (mouseFlg) {
moveFlg  = true;
int[] cell = getCell(table, e.x, e.y);
System.out.println("^^^^^^^^^^^^^^^cell:" + cell[0] + ":" + cell[1]); if (oldCell[0] != cell[0] || oldCell[1] != cell[1]) {
if (oldCell[0] != -1 && oldCell[1] != -1) {
delMoveColor(table);
}
endCell = cell; resetArea(beginCell, endCell);
addMoveColor(table);
oldCell = cell;
}
}
}

});

shell.setSize(600, 400);
shell.setText("設定");
shell.setLayout(new GridLayout(3, false));
shell.layout();
shell.open();
Display display = Display.getDefault();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
} private int[] getCell(Table table, int x, int y) {
int index = table.getTopIndex();
Point pt = new Point(x, y);
int [] point = new int[2];
while (index < table.getItemCount()) {
if (index >=0) {
for (int i = 0; i < table.getColumnCount(); i++) {
final TableItem item = table.getItem(index);
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
point[0] = index;
point[1] = i;
return point;
}
}
}
index++;
}
return point;
}
private Color getPlusColor(Color color1, Color color2) {
int red = (color1.getRed() + color2.getRed())%256;
int green = (color1.getGreen() + color2.getGreen())%256;
int blue = (color1.getBlue() + color2.getBlue())%256;
Color newColor = new Color(null, red, green, blue);
return newColor;

}
private Color getGenColor(Color color1, Color color2) {
int red = (color1.getRed() + (256 - color2.getRed()))%256;
int green = (color1.getGreen() + (256 - color2.getGreen()))%256;
int blue = (color1.getBlue() + (256 - color2.getBlue()))%256;
Color newColor = new Color(null, Math.abs(red), Math.abs(green), Math.abs(blue));
return newColor;
}
private void addMoveColor(Table table) {
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
System.out.println("startCell:" + startCell[0] + ":" + startCell[1]);
System.out.println("endCell:" + endCell[0] + ":" + endCell[1]);
System.out.println("addMoveColor");
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
for (int x = startCell[0]; x < endCell[0] + 1; x++) {
for (int y = startCell[1]; y < endCell[1] + 1; y++) {
TableItem item = table.getItem(x);
System.out.println("Color 追加:" + x + ":"+ y);
item.setBackground(y, getPlusColor(item.getBackground(y), defaultColor));
}
}
}
private void delMoveColor(Table table) {
System.out.println("************************************************");
System.out.println("startCell:" + startCell[0] + ":" + startCell[1]);
System.out.println("endCell:" + endCell[0] + ":" + endCell[1]);
System.out.println("delMoveColor");
System.out.println("************************************************");
for (int x = startCell[0]; x < endCell[0] + 1; x++) {
for (int y = startCell[1]; y < endCell[1] + 1; y++) {
TableItem item = table.getItem(x);
System.out.println("Color 削除:" + x + ":"+ y);
item.setBackground(y, getGenColor(item.getBackground(y), defaultColor));
}
}
}
private void resetArea(int [] newStartCell, int [] newEndCell) {
int startCell0 = newStartCell[0];
int startCell1 = newStartCell[1];
int endCell0 = newEndCell[0];
int endCell1 = newEndCell[1];
int newStartCell0 = 0;
int newStartCell1 = 0;
int newEndCell0 = 0;
int newEndCell1 = 0;

if (startCell0 < endCell0) {
newStartCell0 = startCell0;
newEndCell0 = endCell0;
} else {
newStartCell0 = endCell0;
newEndCell0 = startCell0;
}
if (startCell1 < endCell1) {
newStartCell1 = startCell1;
newEndCell1 = endCell1;
} else {
newStartCell1 = endCell1;
newEndCell1 = startCell1;
}
startCell = new int []{newStartCell0, newStartCell1};
endCell = new int []{newEndCell0, newEndCell1};
}
public static void main(String [] args) {

Map aa = new HashMap();
aa.remove("ss");
new TestTable().open();
// new TestTable().color();
}
}

解决方案 »

  1.   

    建议把table.setHeaderVisible(true);放在初始化table 数据之后
      

  2.   

    阴影是因为那行被选中的默认显示
    你应该结合使用以下事件来完成自定义tableitem的显示:
    SWT.MeasureItem: allows a client to specify the dimensions of a cell's content 
    SWT.EraseItem: allows a client to custom draw a cell's background and/or selection, and to influence whether the cell's foreground should be drawn 
    SWT.PaintItem: allows a client to custom draw or augment a cell's foreground and/or focus rectangle 
    参考http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html
    你设置颜色的逻辑挺复杂的,代码我就不写了。希望上面这篇文章对你有帮助。
      

  3.   

    String布局很难啊,我学的不深,无能为力。