简体   繁体   English

如何在SWT表的列中添加超链接?

[英]How to add Hyperlink in SWT Table`s column?

How to add Hyperlink in SWT Table column ? 如何在SWT表列中添加超链接? I`m using org.eclipse.swt.widgets.Table class. 我正在使用org.eclipse.swt.widgets.Table类。

Is there any way to do this without using TableViewer, JFace ? 有没有办法在不使用TableViewer,JFace的情况下做到这一点?

I tried this way but not working correctly (not showing hyperlinks). 我试过这种方式,但没有正常工作(没有显示超链接)。

for(int i=2; i<4; i++){ 
Hyperlink link = new Hyperlink(table, SWT.WRAP); 
link.setText(temp[i]); 
link.setUnderlined(true);
TableEditor editor = new TableEditor(table); 
editor.setEditor(link, tableItem[index-1], i); //set hyperlinks in column i
}

Below is one way to draw the hyperlink using TableView with a LabelProvider, as mentioned in Tonny Madsen's answer. 下面是使用TableView和LabelProvider绘制超链接的一种方法,如Tonny Madsen的回答中所述。

The code below just paints the hyperlink. 下面的代码只描绘了超链接。

    TableViewerColumn column = ...
    column.setLabelProvider( new MyHyperlinkLabelProvider( tableViewerFiles.getTable() ));

private final class MyHyperlinkLabelProvider extends StyledCellLabelProvider {
    MyHyperlink m_control;

    private MyHyperlinkLabelProvider( Composite parent ) {
        m_control = new MyHyperlink( parent, SWT.WRAP );
    }

    @Override 
    protected void paint( Event event, Object element ) {
        String sValue = ... [Get cell value from row element]
        m_control.setText( sValue );

        GC gc = event.gc;
        Rectangle cellRect = new Rectangle( event.x, event.y, event.width, event.height );
        cellRect.width = 4000;

        m_control.paintText( gc, cellRect);
    }
}

private class MyHyperlink extends Hyperlink {
    public MyHyperlink(Composite parent, int style) {
        super(parent, style);
        this.setUnderlined(true);
    }

    @Override
    public void paintText(GC gc, Rectangle bounds) {
        super.paintText(gc, bounds);
    }
}

Yes, that is certainly possible. 是的,这当然是可能的。 To do this you have to implement SWT.ItemPaint (and possibly also SWT.ItemErase and SWT.ItemMeassure ). 要做到这一点,你必须实现SWT.ItemPaint (可能还有SWT.ItemEraseSWT.ItemMeassure )。

It is easier with TableView though if you use the correct LabelProvider ... 如果使用正确的LabelProvider使用TableView会更容易......

You need to set the size of the editor: 您需要设置编辑器的大小:

editor.grabHorizontal = true;
//or
editor.minimumWidth = 50;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM