1

How to create hyperlinks on a JTextPane. Code Below

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JTextPane;

public class Sample extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sample frame = new Sample();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws BadLocationException 
     */
    public Sample() throws BadLocationException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);

        JLabel lblNewLabel = new JLabel("New label");
        panel.add(lblNewLabel);

        JTextPane textPane = new JTextPane();
        JScrollPane pane = new JScrollPane(textPane);
        contentPane.add(pane, BorderLayout.CENTER);

        textPane.setContentType("text/html");
        textPane.setEditable(false);

        StyledDocument doc = textPane.getStyledDocument();
        doc.insertString(doc.getLength(), "Hello\n", null);
        doc.insertString(doc.getLength(), "<a href=\"\">Cancel</a>\n", null);

    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mercenary
  • 2,106
  • 6
  • 34
  • 43

1 Answers1

3

Read the JEditorPane API for an example of how to add a HyperlinkListener to a JEditorPane or JTextPane.

Edit:

I really don't understand manipulation HTML but I think the following should work:

HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
HTMLDocument doc = (HTMLDocument)textPane.getDocument();
String text = "<a href=\"abc\">hyperlink</a>";
editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • It is not about `HyperLinkListener`. When you try to run that program, I see html tags instead of hyperlinks. – Mercenary Mar 31 '14 at 15:15
  • @Mercenary: Some alternatives are suggested [here](http://stackoverflow.com/a/16447176/230513). – trashgod Mar 31 '14 at 15:30
  • @Mercenary, you really should be using a JEditorPane for displaying HTML. The API has a link to the Swing tutorial. See edit for some code for dynamically adding text. – camickr Mar 31 '14 at 15:34
  • @camickr Just one more thing...how to check for the name 'hyperlink' as in your example in `HyperlinkListener`. I basically have two links - Save and Cancel. I need to do some action separately for Save and Cancel – Mercenary Apr 01 '14 at 17:21