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?

2
  • Do you use a simple Table or a TableViewer?
    – Baz
    Sep 25, 2012 at 14:06
  • @Baz A Table, but if it would be much easier with TableViewer I guess I could change it. Sep 25, 2012 at 14:10

2 Answers 2

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);
        }
    }
});
4
  • What if the table is oriented vertically? Is there a way to do this for a row? Oct 1, 2012 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 1, 2012 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. Oct 1, 2012 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 1, 2012 at 15:27
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.