0

How can I add a hyperlink to a SWT Table?

I need to have a table with ordinary TableItem objects as its rows, but sometimes I need to have a hyperlink there, so that someone can click on it open the linked page from application level.

Any tips on how to achieve this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marcin Krzysiak
  • 239
  • 1
  • 5
  • 13

2 Answers2

0

Here is my answer to your question: Instead of a Button, add a Hyperlink.

SWT - Tableviewer adding a remove button to a column in the table

Community
  • 1
  • 1
sambi reddy
  • 3,065
  • 1
  • 13
  • 10
0

As an alternative to sambi reddy's answer, you can use a StyledCellLabelProvider (if you switch to a TableViewer) for your column and use a StyledString to represent your link. Of course, you will have to handle the mouse events yourself.

Here is an example:

// Column for the link
TableViewerColumn col2 = createTableViewerColumn("Link", 100, 1, viewer);
col2.setLabelProvider(new StyledCellLabelProvider() {
    @Override
    public void update(ViewerCell cell)
    {
        Object element = cell.getElement();
        if(element instanceof Person)
        {
            Person person = (Person) cell.getElement();

            /* make text look like a link */
            StyledString text = new StyledString();
            StyleRange myStyledRange = new StyleRange(0, person.getLocation().length(), Display.getCurrent().getSystemColor(SWT.COLOR_BLUE), null);
            myStyledRange.underline = true;
            text.append(person.getLocation(), StyledString.DECORATIONS_STYLER);
            cell.setText(text.toString());

            StyleRange[] range = { myStyledRange };
            cell.setStyleRanges(range);

            super.update(cell);
        }
    }
});
Baz
  • 36,440
  • 11
  • 68
  • 94
  • What if the table is oriented vertically? Is there a way to do this for a row? – Marcin Krzysiak Oct 01 '12 at 11:09
  • @MarcinKrzysiak Are you using a `TableViewer`? Are the objects ordered in rows or columns? If they are ordered in rows, the example above will do what you want. – Baz Oct 01 '12 at 11:14
  • The objects are ordered in columns. Each column is a separate object. I don't use `TableViewer` yet, but if the case I'm writing about is doable, I will. If not, I might consider to change my project's layout a bit. – Marcin Krzysiak Oct 01 '12 at 12:52
  • @MarcinKrzysiak I couldn't find anything on how to display you objects as columns... Which doesn't mean that it's not possible. Why do you prefer to organise them this way? Any particular reason? – Baz Oct 01 '12 at 15:27