Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to put a hyperlink to SWT Label Text in Java

Posted on 2013-11-22
2
Medium Priority
?
1,507 Views
Last Modified: 2013-11-26
Hi,
How can I add a hyperlink to JAVA SWT  Label text.

When I click on this text in the label, I want the defaul browser to navigate to this site.


This is the code I haver and I want the "Help" to be link to a website:

        GridData gridDataBanner2 = new GridData(GridData.HORIZONTAL_ALIGN_END);
        CLabel banner2 = new CLabel(shell, SWT.CENTER);
        Color MtGreen = new Color (shell.getDisplay(), 240,240,240);
        banner2.setBackground(MtGreen); 
        
        FontData fontData2 = banner2.getFont().getFontData()[0];
        Font fontBanner2 = new Font(shell.getDisplay(), new FontData(fontData2.getName(),8, SWT.NORMAL));
        banner2.setFont(fontBanner2);
        banner2.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
        

        banner2.setText("Help");
        banner2.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_BLUE));
        banner2.setLayoutData(gridDataBanner2);

Open in new window



Thanks,
0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:Tolgar
ID: 39673007
Any ideas?
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39676342
I don't think SWT has anything "in-built" to do this, but you should be able to fake it. It looks like you have already started on that, setting the color of the text used. You might also want to make it underlined.

Then you need to make it act like a link. For that it probably should change cursor when hover over it, so...
banner2.setCursor(display.getSystemCursor(SWT.CURSOR_HAND));

Open in new window


And make it DO something when you click it...
banner2.addMouseListener(new MouseAdapter() {
    public void mouseUp(MouseEvent event) {
        if (event.button == 1) {   // Left button pressed & released
            Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                try {
                    desktop.browse("http://address.of.your.website.here.com");
                } catch (Exception e) {
                    // Handle any exceptions appropriately
                }
            }
        }
    }
}

Open in new window

(Note the above should work for Windows, but it's probably OS dependant and I don't have any others to test on, so I will leave that up to you to try it on others if you need.)

(Also note, that Desktop is not a SWT class, you would need to   import java.awt.Desktop;    at the top of your .java file to use it)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The viewer will learn how to implement Singleton Design Pattern in Java.
Introduction to Processes
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question