swt中的table单元格渐变颜色

转自http://www.blogjava.net/kingLv/articles/239354.html


1public class PaintTableCell {
 2
 3    public static void main(String[] args) {
 4        final Display display = Display.getDefault();
 5        Shell shell = new Shell(display);
 6        shell.setText("Paint Table Cell");
 7        shell.setSize(500400);
 8        shell.setLayout(new FillLayout());
 9        
10        final Table table = new Table(shell, SWT.FULL_SELECTION | SWT.SINGLE);
11        table.setHeaderVisible(true);
12        table.setLinesVisible(true);
13        
14        TableColumn column1 = new TableColumn(table, SWT.NONE);
15        column1.setWidth(100);
16        column1.setAlignment(SWT.LEFT);
17        column1.setText("Column 1");
18        
19        TableColumn column2 = new TableColumn(table, SWT.NONE);
20        column2.setWidth(100);
21        column2.setAlignment(SWT.RIGHT);
22        column2.setText("Column 2");
23        
24        TableItem item1 = new TableItem(table, SWT.NONE);
25        item1.setText("item 11");
26        
27        TableItem item2 = new TableItem(table, SWT.NONE);
28        item2.setText(new String[] {"item 21""item 22"});
29        
30        table.addPaintListener(new PaintListener() {
31
32            @Override
33            public void paintControl(PaintEvent e) {
34                //获得单元格的位置
35                Rectangle rect = table.getItem(0).getBounds(1);
36                
37                //计算需要绘制渐变效果的长度
38                int width = rect.width * 67 / 100;//67%
39                
40                GC gc = e.gc;
41                //设置GC的背景色和前景色
42                gc.setForeground(new Color(display, 2551920));
43                gc.setBackground(new Color(display, 255230151));
44                //绘制渐变效果
45                gc.fillGradientRectangle(rect.x, rect.y, width, rect.height, false);
46                
47                //绘制单元格中的文字
48                String text = "item 12";
49                gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
50                int pixels = gc.getFontMetrics().getAverageCharWidth() * text.length();
51                gc.drawString(text, rect.x + rect.width - pixels - 6, rect.y, true);
52            }

53            
54        }
);    
55        
56        shell.open();
57        
58        while(!shell.isDisposed()) {
59            if(!display.readAndDispatch()) {
60                display.sleep();
61            }

62        }

63        display.dispose();
64    }

65}

66


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值