-1

I'm writing a UI using Java Swing, and I need to have a picture that, when clicked, takes the user to a specific webpage.

So far, the other images I've been adding to my project have been done using

JLabel myImage = new JLabel(new ImageIcon("filePath\\imagename.png"));

But now that this specific image needs to contain a hyperlink, I can't figure out whether it's possible to embed a link using the above JLabel/ImageIcon approach.

As an alternative approach, I thought an HtmlPanel would be the way to go, but I can't get an image to show that way. The following code gives me a broken image, but which is clickable and takes me to my webpage:

HtmlPanel myImage = new HtmlPanel("<a href='http://myWebsite.com'> <img src='filepath\\fileName.png' alt='blahblah'></a>");

Any advice would be greatly appreciated! (P.S. I'm relatively new to programming in general, so if your answers could contain example code, that would be extra helpful, since most people's answers go way over my head)

BigWeirdAlFan
  • 105
  • 1
  • 7
  • *"I thought an HtmlPanel would be the way to go"* What is an `HtmlPanel`? I don't see it in JSE 8 docs. – Andrew Thompson Jun 25 '15 at 14:10
  • 1
    BTW - a `JEditorPane` supports images, links and images that are links. – Andrew Thompson Jun 25 '15 at 14:11
  • Check out this post - http://stackoverflow.com/questions/527719/how-to-add-hyperlink-in-jlabel – Amber Jun 25 '15 at 14:19
  • Oh, sorry, I forgot that an HtmlPanel wasn't an official thing. It's a class my friend made up that extends JEditorPane. So, since it is a JEditorPane and you mentioned that JEditorPane supports images that are links, do you know how to combine the image and the link using JEditorPane? – BigWeirdAlFan Jun 25 '15 at 14:53
  • You probably want to add a MouseListener to your JLabel. Read the [How to Write a Mouse Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) tutorial. – VGR Jun 25 '15 at 15:21
  • *"how to combine the image and the link"* This is basic HTML! *"..using JEditorPane?"* Did you try reading The Fine Manual as to how to make a link work? There is an example at the top of the docs! Seriously, in future, try something and show what you've tried before asking to be spoon fed information that is in the documentation or otherwise freely available. – Andrew Thompson Jun 25 '15 at 15:24

1 Answers1

0

For this you have to add mouse listener in JLabel label which contains ImageIcon means your image.

Here is the Code :

    Icon i1 = new ImageIcon(getClass().getResource("image.jpg"));
    l4 = new JLabel(i1);

    l4.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {

            String command = "rundll32 url.dll,FileProtocolHandler https://www.google.com";
            try {
                Process p = Runtime.getRuntime().exec(command);

            } catch (Exception e1) {

                JOptionPane.showMessageDialog(null, "\n Error! \n");

            }

        }
    });
    add(l4);
Programmer
  • 445
  • 4
  • 12
  • Thanks a million! This was incredibly helpful, and I really appreciated getting a specific example. If I had enough reputation points, I'd upvote your answer :-) – BigWeirdAlFan Jun 25 '15 at 17:43