Solved

How to put a hyperlink to SWT Label Text in Java

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now