Clickable HTML link in JEditorPane

xaxes picture xaxes · Feb 13, 2013 · Viewed 8.6k times · Source

I'd like to make all links in JEditorPane clickable. I tried to use the code from this answer, but probably I've done something wrong because nothing happens when I click on the link. Here's my code:

JEditorPane news = new JEditorPane();
news.setSize(Size.L_NEWS);
news.setLocation(Position.L_NEWS);
news.setFocusable(false);
news.setBackground(new Color(255, 255, 255, 0));
news.setEditable(false);
news.setEnabled(false);
news.setOpaque(false);
news.setVisible(true);
news.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
news.setText(getNewsHTML.getNewestNews());
try{
    UIManager.setLookAndFeel(
    UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e){
    e.printStackTrace();
}
news.addHyperlinkListener(new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            if(Desktop.isDesktopSupported()) {
                try {
                    Desktop.getDesktop().browse(e.getURL().toURI());
                }
                catch (IOException | URISyntaxException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
);
login_form.add(news);

And here - my imports(maybe they are the problem):

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

What is wrong? I don't have any output to Eclipse's console.

Answer

David Kroukamp picture David Kroukamp · Feb 13, 2013

From my comments:

See my simple example here:

http://i.stack.imgur.com/SglJF.png

Your problem lies here:

news.setEnabled(false);

Dont set it to disabled or it wont be able to catch events like mouse click etc.

Also not sure why you have:

news.setFocusable(false);
news.setVisible(true);

The component does not need to be set visible just add to a container and make the container visible. Also dont make it unfocusable as this may cause problems later. Your setEditable(false) should be enough (as user wont be able to edit it regardless of focusabilty)