How to add Hyperlink in SWT Table`s column?

纵饮孤独 提交于 2019-11-30 05:25:31

问题


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

Is there any way to do this without using 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
}

回答1:


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

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




回答2:


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

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:


You need to set the size of the editor:

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


来源:https://stackoverflow.com/questions/6981638/how-to-add-hyperlink-in-swt-tables-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!