A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© jttsai 山林野马   /  2013-7-25 11:04  /  767 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

table并不像网页那样可以任意复制,需要对table进行监听并对想要复制的内容进行任意复制
希望大家能帮助到大家。人生在此,催我远行,共勉之~
  1. //对表格进行监听
  2.          table.addMouseListener(new MouseAdapter() {
  3.                         public void mouseDown(MouseEvent e) {
  4.                                 table.setData("Point", new Point(e.x, e.y));
  5.                         }
  6.                 });
  7.                  //进行复制
  8.                 MenuManager menuManager = new MenuManager("");
  9.                 menuManager.add(new Action("复制") {
  10.                         public void run() {

  11.                                 TableItem item = table.getSelection()[0];

  12.                                 for (int i = 0; i < table.getColumnCount(); i++) {
  13.                                         Rectangle rect = item.getBounds(i);
  14.                                         if (rect.contains((Point) table.getData("Point"))) {
  15.                                                 Clipboard clipboard = new Clipboard(
  16.                                                                 TallyProcessingView.this.getShell().getDisplay());

  17.                                                 String text = item.getText(i);

  18.                                                 TextTransfer textTransfer = TextTransfer.getInstance();

  19.                                                 clipboard.setContents(new String[] { text },
  20.                                                                 new Transfer[] { textTransfer });

  21.                                                 clipboard.dispose();

  22.                                                 break;
  23.                                         }
  24.                                 }
  25.                         }
  26.                 });

  27.                 final Menu menu = menuManager.createContextMenu(table);
  28.                 table.setMenu(menu);

  29.                 menu.addMenuListener(new MenuListener() {
  30.                         public void menuHidden(MenuEvent arg0) {
  31.                                 // TODO Auto-generated method stub

  32.                         }

  33.                         public void menuShown(MenuEvent arg0) {

  34.                         }
  35.                 });
复制代码

评分

参与人数 1技术分 +1 收起 理由
特殊服务 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马