SWT TableEditor在Ubuntu中不显示文本框

时间:2015-02-16 22:58:50

标签: java swt

我正在SWT中构建一个表,需要将一列作为文本字段进行编辑。我从http://www.java2s.com/获取了示例并将它们编码到我的应用程序中。在OSX Yosemite中,这可行:当单击表格单元格时,单元格将变为文本框,其行为类似于文本框。

当在Ubuntu 14.04(OpenJDK 8)中使用相同的代码时,文本框不会出现,但编辑过程在下面工作。为了测试它不是我的示例版本,我在这个环境中逐字运行了示例,它完全相同。示例中的代码是:

  public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
        table.setLinesVisible(true);
        for (int i = 0; i < 3; i++) {
          TableColumn column = new TableColumn(table, SWT.NONE);
          column.setWidth(100);
        }
        for (int i = 0; i < 3; i++) {
          TableItem item = new TableItem(table, SWT.NONE);
          item.setText(new String[] { "" + i, "" + i, "" + i });
        }
        final TableEditor editor = new TableEditor(table);
        editor.horizontalAlignment = SWT.LEFT;
        editor.grabHorizontal = true;
        table.addListener(SWT.MouseDown, new Listener() {
          public void handleEvent(Event event) {
            Rectangle clientArea = table.getClientArea();
            Point pt = new Point(event.x, event.y);
            int index = table.getTopIndex();
            while (index < table.getItemCount()) {
              boolean visible = false;
              final TableItem item = table.getItem(index);
              for (int i = 0; i < table.getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {
                  final int column = i;
                  final Text text = new Text(table, SWT.NONE);
                  Listener textListener = new Listener() {
                    public void handleEvent(final Event e) {
                      switch (e.type) {
                      case SWT.FocusOut:
                        item.setText(column, text.getText());
                        text.dispose();
                        break;
                      case SWT.Traverse:
                        switch (e.detail) {
                        case SWT.TRAVERSE_RETURN:
                          item
                              .setText(column, text
                                  .getText());
                        //FALL THROUGH
                        case SWT.TRAVERSE_ESCAPE:
                          text.dispose();
                          e.doit = false;
                        }
                        break;
                      }
                    }
                  };
                  text.addListener(SWT.FocusOut, textListener);
                  text.addListener(SWT.Traverse, textListener);
                  editor.setEditor(text, item, i);
                  text.setText(item.getText(i));
                  text.selectAll();
                  text.setFocus();
                  text.redraw(); text.update();
                  if (text.isFocusControl())
                      System.out.println("focussed");
                  return;
                }
                if (!visible && rect.intersects(clientArea)) {
                  visible = true;
                }
              }
              if (!visible)
                return;
              index++;
            }
          }
        });
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch())
            display.sleep();
        }
        display.dispose();
      }

1 个答案:

答案 0 :(得分:1)

更改文本框样式。将第final Text text = new Text(table, SWT.NONE);行替换为final Text text = new Text(table, SWT.BORDER);

当使用行final Text text = new Text(table, SWT.NONE);时,它看起来是:

enter image description here

当使用行final Text text = new Text(table, SWT.BORDER);时,它看起来是:

enter image description here