在Eclipse RCP 设置表格的行高、背景颜色以及字体等等

    基于RCP平台写程序的时候,经常遇到需要设置table行高的问题。Table和TableItem以及TableViewer类中都没有相应的方法可用。于是综合了一下网友的智慧,找到了几个设置表格控件行高的方法

    第一种,通过设置指定height的Image来改变行高,代码演示如下:

  1. Display display = new Display();  
  2. Shell shell = new Shell(display);  
  3. shell.setBounds(1010200250);  
  4. Table table = new Table(shell, SWT.NONE);  
  5. table.setBounds(1010150200);  
  6. table.setLinesVisible(true);  
  7. int rowHeight =30;  
  8.          
  9. TableItem item1 =new TableItem(table, SWT.NONE);  
  10. item1.setText("item 1");  
  11. TableItem item2 =new TableItem(table, SWT.NONE);  
  12. item2.setText("item 2");  
  13. TableItem item3 =new TableItem(table, SWT.NONE);  
  14. item3.setText("item 3");  
  15.          
  16. Image image = new Image(display, 1, rowHeight);  
  17. item1.setImage(image);  
  18. shell.open();  
  19. while (!shell.isDisposed()) {  
  20.     if (!display.readAndDispatch())  
  21.         display.sleep();  
  22.     }  
  23. display.dispose();  

不过就算我只在item1上setImage,item2和item3行高也会一起改变,没法单独指定某一行的高度。

        第二种,是通过捕获SWT.MeasureItem事件来定制单元格的高度和宽度,具体内容可参看原文Custom Drawing Table and Tree Items,代码演示如下:

  1. Display display = new Display();  
  2. Shell shell = new Shell(display);  
  3. shell.setBounds(1010200250);  
  4. final Table table = new Table(shell, SWT.NONE);  
  5. table.setBounds(1010150200);  
  6. table.setLinesVisible(true);  
  7. final int rowHeight =30;  
  8.          
  9. for (int i = 0; i < 5; i++) {  
  10.     new TableItem(table, SWT.NONE).setText("item " + i);  
  11. }  
  12.      
  13. table.addListener(SWT.MeasureItem, new Listener() {  
  14.     public void handleEvent(Event event) {  
  15.         event.height =rowHeight;  
  16.     }  
  17. });  
  18.          
  19. shell.open();  
  20. while (!shell.isDisposed()) {  
  21.     if (!display.readAndDispatch())  
  22.     display.sleep();  
  23. }  
  24. display.dispose();   

        同样的,这个方法也不能为每一行指定不同的行高,在原文中也说了"All items in a table have the same height, so increasing the height of a cell will result in the height of all items in the table growing accordingly."。看来这一点在SWT中是做不到了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值