table并不像网页那样可以任意复制,需要对table进行监听并对想要复制的内容进行任意复制
希望大家能帮助到大家。人生在此,催我远行,共勉之~- //对表格进行监听
- table.addMouseListener(new MouseAdapter() {
- public void mouseDown(MouseEvent e) {
- table.setData("Point", new Point(e.x, e.y));
- }
- });
- //进行复制
- MenuManager menuManager = new MenuManager("");
- menuManager.add(new Action("复制") {
- public void run() {
- TableItem item = table.getSelection()[0];
- for (int i = 0; i < table.getColumnCount(); i++) {
- Rectangle rect = item.getBounds(i);
- if (rect.contains((Point) table.getData("Point"))) {
- Clipboard clipboard = new Clipboard(
- TallyProcessingView.this.getShell().getDisplay());
- String text = item.getText(i);
- TextTransfer textTransfer = TextTransfer.getInstance();
- clipboard.setContents(new String[] { text },
- new Transfer[] { textTransfer });
- clipboard.dispose();
- break;
- }
- }
- }
- });
- final Menu menu = menuManager.createContextMenu(table);
- table.setMenu(menu);
- menu.addMenuListener(new MenuListener() {
- public void menuHidden(MenuEvent arg0) {
- // TODO Auto-generated method stub
- }
- public void menuShown(MenuEvent arg0) {
- }
- });
复制代码 |