Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyperlinks in JLabels

This is the code I have in a file called Test2.java in a package called test2 in a project called Test2;

package test2;

import javax.swing.JFrame;

public class Test2 {
    public static void main(String[] args) {
    JFrame mainWindow = new HtmlWindow("<html>"
            + "<a href=\"http://stackoverflow.com\">"
            + "blah</a></html>");
        mainWindow.setVisible(true);
    }
}

In the same package I have this code in a file called HtmlWindow.java ;

package test2;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;


class HtmlWindow extends JFrame {
    public HtmlWindow(String refreshGrid) {
        super("blah");
        setSize(300, 100);
        Container content = getContentPane();
        String labelText = refreshGrid;
        JLabel coloredLabel = new JLabel (labelText, JLabel.CENTER); 
        content.add(coloredLabel, BorderLayout.NORTH);

    }

}

When I run the project, I get a window with the word "blah" in the expected location, blue and underlined, but the cursor does not change when I hover over the word, nor does anything happen when I click on it.

My questions are as follows;

  • Is it possible to put hyperlinks in jLabels?
  • If so, am I doing something wrong, or is the programme not running properly?
  • If not, what is a good alternative? The reason I am not using JButtons, is that eventually I want to create a grid of an arbitrary number of hyperlinks, and I want the hyperlinks to be images, and whilst JButtons can have images on them, I don't want the hyperlinks to look like "buttons". I suppose I could use a non-editable JEditorPane?
like image 447
George Simms Avatar asked Jul 26 '12 12:07

George Simms


People also ask

How do I add a hyperlink to a JLabel?

Click the link and the default browser will be launched to open the hyperlink. * A hyperlink component that is based on JLabel. * This Java Swing program demonstrates how to use JHyperlink custom component. Click “Visit our website” and the default browser opens the website.

How do you add a hyperlink in Java?

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

How do you make text clickable in Java?

If you are using a JTextPane you can use the insertComponent() method to insert a new JLabel that is of the same font as the JTextPane font and you can customize the JLabel the way you want like setting the cursor to hand cursor thereby giving it a clickable look and feel. Save this answer. Show activity on this post.


1 Answers

Swing is not a fully functioned browser. It supports simple HTML including links but do not change the cursor style automatically. As far as I know you have to do it programmatically, i.e. add mouse listener to your label and change the cursor style when your mouse is over your label. Obviously you can write your own class LinkLabel that implements this logic and then use it every time you need.

like image 117
AlexR Avatar answered Oct 23 '22 09:10

AlexR