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;
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.
Creating a HyperlinkHyperlink link = new Hyperlink(); link. setText("http://example.com"); link. setOnAction((ActionEvent e) -> { System. out.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With