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

时间:2023-01-20 21:44:22

How to add Hyperlink in SWT Table column ? I`m using org.eclipse.swt.widgets.Table class.

如何在SWT表列中添加超链接?我用org.eclipse.swt.widgets。表类。

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
}

3 个解决方案

#1


2  

Yes, that is certainly possible. To do this you have to implement SWT.ItemPaint (and possibly also SWT.ItemErase and SWT.ItemMeassure).

是的,那当然是可能的。为此,您必须实现SWT。它不是(也可能是SWT。ItemErase和SWT.ItemMeassure)。

It is easier with TableView though if you use the correct LabelProvider...

使用TableView会更容易,如果你使用了正确的LabelProvider。

#2


4  

Below is one way to draw the hyperlink using TableView with a LabelProvider, as mentioned in Tonny Madsen's answer.

下面是一种使用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);
    }
}

#3


0  

You need to set the size of the editor:

您需要设置编辑器的大小:

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

#1


2  

Yes, that is certainly possible. To do this you have to implement SWT.ItemPaint (and possibly also SWT.ItemErase and SWT.ItemMeassure).

是的,那当然是可能的。为此,您必须实现SWT。它不是(也可能是SWT。ItemErase和SWT.ItemMeassure)。

It is easier with TableView though if you use the correct LabelProvider...

使用TableView会更容易,如果你使用了正确的LabelProvider。

#2


4  

Below is one way to draw the hyperlink using TableView with a LabelProvider, as mentioned in Tonny Madsen's answer.

下面是一种使用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);
    }
}

#3


0  

You need to set the size of the editor:

您需要设置编辑器的大小:

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