SWT表和超链接小部件集成 [英] SWT Table and Hyperlink widget integration

查看:89
本文介绍了SWT表和超链接小部件集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SWT Table和Hyperlink的时间过长。我已经阅读了关于stackoverflow和JFace代码段以及Eclipse SWT代码段的所有主题,但是找不到解决我问题的答案。

I fight with SWT Table and Hyperlink for too long time. I've read all of the topics on stackoverflow and JFace Snippets and Eclipse SWT Snippets and I couldn't find an answer to my problem.

我尝试创建SWT表每行包含 DELETE超链接的列。
我的问题是我无法使其正常显示。

I try to create SWT Table with column that contains "DELETE" hyperlink in every row. My problem is that I can't make it look ok.

也许我将代码放在第一位:

Maybe I will put my code first:

import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.forms.widgets.Hyperlink;

public class TableWithDelete {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    Composite composite = new Composite(shell, SWT.NONE);
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    TableColumnLayout tableLayout = new TableColumnLayout();
    composite.setLayout(tableLayout);

    TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL);
    Table table = tableViewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(false);

    TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.BORDER);
    TableColumn columnWeight = column.getColumn();
    tableLayout.setColumnData(columnWeight, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
    columnWeight.setText("Name");

    column = new TableViewerColumn(tableViewer, SWT.NONE);
    columnWeight = column.getColumn();
    tableLayout.setColumnData(columnWeight, new ColumnWeightData(1, ColumnWeightData.MINIMUM_WIDTH, true));
    columnWeight.setText("Removal");

    //fill with some data

    String[] sampleNames = new String[]{"Test 1", "Test 2", "Test 3", "Test 4", "Test 5"};

    for (String name : sampleNames) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(0, name);
    }

    TableItem[] items = table.getItems();
    final List<TableEditor> editors = new ArrayList<TableEditor>();
    for (int i = 0; i < items.length; i++) {
        TableEditor editor = new TableEditor(table);
        Hyperlink hyperlink = new Hyperlink(table, SWT.NONE);
        hyperlink.setText("DELETE");
        hyperlink.pack();
        hyperlink.addListener(SWT.MouseDown, new Listener() {

            @Override
            public void handleEvent(Event event) {
                System.out.println("Some action on DELETE");
            }
        });
        editor.minimumWidth = hyperlink.getSize().x;
        editor.setEditor(hyperlink, items[i], 1);
        editors.add(editor);
    }
    shell.setSize(300, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }

}

我的问题是,如果我跑步该代码,我检索到超链接的丑陋默认外观:

My problem is that if I run that code, I retrieve ugly default look of the hyperlink:

当我更改样式时,将超链接传递给SWT.TRANSPARENT:

When I change style that I pass to Hyperlink to SWT.TRANSPARENT:

Hyperlink hyperlink = new Hyperlink(table, SWT.TRANSPARENT);

比表中的行看起来不错,但是透明度似乎传递给了其他表部分,并且由于该表格的边框很难看:

than row in table starts to look ok, but "transparency" seems to be passed to other table parts, and because of that border of the table looks ugly:

还有,当我将鼠标移到超链接上时,表的当前行不再突出显示。

and what's more when I move mouse over Hyperlink, current row of the table is not highlighted anymore.

我想念什么?如何使SWT表能够显示单元格中的链接(不仅看起来像超链接,而且鼠标光标已正确更改)?

What do I miss? How to make SWT Table able to show link in a cell (That does not only look like a hyperlink but also has Mouse cursor correctly changed)?

我将非常感谢寻求帮助。

I will be very grateful for any help.

推荐答案

您可能想看看

SWT-Tableviewer添加

代替按钮使用超链接

这篇关于SWT表和超链接小部件集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆