Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create programs in Java that create text to link in Chrome?

I apologize for the long question.

I was browsing a forum the other day and I saw a few pieces of text that were linking to youtube and other sites. I had to always highlight and then copy and paste or right click "go to" in google chrome browser.

Since I've been playing with Java a little bit, I thought about making my own little program that will give a link to text that has an address . For example if I said "hey, check this video out I saw the other day 'www.youtube.com' " I'd want the youtube part to be clickable.

Could anybody tell me if such a thing is possible and if it is, what libraries would I have to use for this and lastly, how can I find a list of all imports and libraries in java?

Thanks.

like image 724
Lion Avatar asked Jan 05 '13 08:01

Lion


People also ask

How do you make a clickable link in Java?

Creating a Hyperlink Hyperlink link = new Hyperlink(); link. setText("http://example.com"); link. setOnAction((ActionEvent e) -> { System.

How do you hyperlink text?

Create a hyperlink to a location on the webSelect the text or picture that you want to display as a hyperlink. Press Ctrl+K. You can also right-click the text or picture and click Link on the shortcut menu. In the Insert Hyperlink box, type or paste your link in the Address box.

How do I view Java code in Chrome?

Edge/Internet Explorer: CTRL + U. Or right click and select “View Source.” Chrome: CTRL + U.


1 Answers

Use HTML in JEditorPane and add HyperLinkListener to detect click on URLs.

Than use Desktop API to open default browser with the URL.

Something like:

enter image description here

import java.awt.Desktop;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class Test {

    public static void main(String[] argv) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JEditorPane jep = new JEditorPane();
                jep.setContentType("text/html");//set content as html
                jep.setText("Welcome to <a href='http://stackoverflow.com/'>StackOverflow</a>.");

                jep.setEditable(false);//so its not editable
                jep.setOpaque(false);//so we dont see whit background

                jep.addHyperlinkListener(new HyperlinkListener() {
                    @Override
                    public void hyperlinkUpdate(HyperlinkEvent hle) {
                        if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
                            System.out.println(hle.getURL());
                            Desktop desktop = Desktop.getDesktop();
                            try {
                                desktop.browse(hle.getURL().toURI());
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                });


                JFrame f = new JFrame("HyperlinkListener");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(jep);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}
like image 52
David Kroukamp Avatar answered Nov 15 '22 04:11

David Kroukamp