SWT的Table表格可编辑单元格

  final Table table = new Table(parent, SWT.NONE | SWT.FULL_SELECTION);
  final GridData gd = new GridData(SWT.LEFT, SWT.TOP, false, true);
  gd.heightHint = 100;
  table.setLayoutData(gd);
  table.setHeaderVisible(true);
  table.setLinesVisible(true);
  final TableColumn tcCondition = new TableColumn(table, SWT.None);
  final TableColumn tcUserValue = new TableColumn(table, SWT.None);
  final TableColumn tcUserColor = new TableColumn(table, SWT.None);
  tcCondition.setText("条件");
  tcCondition.setWidth(80);
  tcUserValue.setText("枚举值");
  tcUserValue.setWidth(80);
  tcUserColor.setText("颜色");
  tcUserColor.setWidth(80);
  table.addListener(SWT.MouseDoubleClick, new Listener() {

   int editColumnIndex = -1;

   @Override
   public void handleEvent(final Event e) {
    final Point point = new Point(e.x, e.y);
    final TableItem tableItem = table.getItem(point);
    if (tableItem == null) {
     return;
    }
    for (int i = 0; i < 3; i++) {
     final Rectangle r = tableItem.getBounds(i);
     if (r.contains(point)) {
      editColumnIndex = i;
      final TableEditor editor = new TableEditor(table);
      final Control oldEditor = editor.getEditor();
      if (oldEditor != null) {
       oldEditor.dispose();
      }
      final Text text = new Text(table, SWT.NONE);
      text.computeSize(SWT.DEFAULT, table.getItemHeight());
      editor.grabHorizontal = true;
      editor.minimumHeight = text.getSize().y;
      editor.minimumWidth = text.getSize().x;
      editor.setEditor(text, tableItem, editColumnIndex);
      text.setText(tableItem.getText(editColumnIndex));
      text.forceFocus();
      text.addModifyListener(new ModifyListener() {

       @Override
       public void modifyText(final ModifyEvent e) {
        editor.getItem().setText(editColumnIndex, text.getText());

       }
      });
      text.addFocusListener(new FocusListener() {

       @Override
       public void focusGained(final FocusEvent e) {
        // TODO Auto-generated method stub

       }

       @Override
       public void focusLost(final FocusEvent e) {
        text.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
        text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
       }
      });
     }
    }

   }
  });

另一种方法参考http://blog.csdn.net/andywangcn/article/details/8117288




  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Java 的 SWT 中,要使表格Table)中的数据可以复制,您需要为表格添加复制功能的相关代码。以下是一个示例代码,演示如何实现表格数据的复制功能: ```java import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.*; import org.eclipse.swt.layout.FillLayout; 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 TableCopyExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); // 创建表格 Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); table.setHeaderVisible(true); // 添加表格TableColumn column1 = new TableColumn(table, SWT.NONE); column1.setText("Column 1"); TableColumn column2 = new TableColumn(table, SWT.NONE); column2.setText("Column 2"); // 添加表格项 for (int i = 0; i < 10; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "Data " + (i + 1) + ", 1", "Data " + (i + 1) + ", 2" }); } // 设置表格的复制剪贴板操作 table.addListener(SWT.KeyDown, event -> { if (event.stateMask == SWT.CTRL && event.keyCode == 'c') { StringBuilder sb = new StringBuilder(); TableItem[] selection = table.getSelection(); for (TableItem item : selection) { for (int i = 0; i < table.getColumnCount(); i++) { sb.append(item.getText(i)).append("\t"); } sb.append("\n"); } // 将复制的数据放入剪贴板 if (sb.length() > 0) { TextTransfer textTransfer = TextTransfer.getInstance(); Clipboard clipboard = new Clipboard(display); clipboard.setContents(new Object[] { sb.toString() }, new Transfer[] { textTransfer }); clipboard.dispose(); } } }); // 调整表格列宽 column1.pack(); column2.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } ``` 这个示例代码创建了一个带有两列的表格,并添加了一些数据。通过按下 `Ctrl+C` 键,可以将选中的表格数据复制到剪贴板中,您可以在其他应用程序中粘贴这些数据。 请注意,这个示例只提供了基本的复制功能,如果您需要更复杂的功能,比如支持表格中的式、富文本等,请参考 SWT 的更高级特性和相应的库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值