Solved

How to put a hyperlink to SWT Label Text in Java

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Rhino Java Array in Javascript 1 59
ArrayList Adding/settign data 3 38
What is the use of Forwarding Class in java 1 54
Java regex 5 22
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This video teaches viewers about errors in exception handling.

752 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