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
Solved

How to put a hyperlink to SWT Label Text in Java

Posted on 2013-11-22
2
1,322 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
2 Comments
 

Author Comment

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

Accepted Solution

by:
mccarl earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This video teaches viewers about errors in exception handling.

809 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