Hello there,

I am using the JTextPane in my Java Swing Application. How do I make it Handle Hypelinks. Like lets say the use enters

\\some_foldername\some_subfolder_foldername\some_file.xls or any other file type, It should Automatically, treat it as a hyperlink.

Is it Possible to have such a functionlaity

JTextPane Hyperlink Functionality

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class TextSamplerDemo extends JPanel {

	public TextSamplerDemo() {
		setLayout(new BorderLayout());

		// Create a text pane.
		JTextPane textPane = new JTextPane();
		setPreferredSize(new Dimension(250, 155));
		setMinimumSize(new Dimension(10, 10));
		// Put everything together.
		add(textPane, BorderLayout.LINE_START);

	private static void createAndShowGUI() {
		JFrame frame = new JFrame("Hyperlinks in Java Editors");

		frame.add(new TextSamplerDemo());

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				UIManager.put("swing.boldMetal", Boolean.FALSE);

Recommended Answers

All 3 Replies

It is possible but it will take a long time to implement. So first try your self. If you need help afterward it will be delievered.
you add something like if user select the text and give a the location by third click options. Then the text selected may be given a onclick event via mouselistener. And as well you can change text color also.
this the the idea i having you can think yourself.
try it, its seems very interesting.

Well you may allow the user to enter the text and the URL in a dialog. Then implement a method -

private int linkID = 0;
public void addHyperlink(URL url, String text) {
  try {
    SimpleAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setUnderline(attrs, true);
    StyleConstants.setForeground(attrs, unvisitedColor);
    attrs.addAttribute(ID, new Integer(++linkID));
    attrs.addAttribute(HTML.Attribute.HREF, url.toString());
    doc.insertString(doc.getLength(), text, attrs);
  catch (BadLocationException e) {

Then also define a mouse and mouse motion listener and do -

public class LinkController extends MouseAdapter 
                            implements MouseMotionListener {

  public void mouseMoved(MouseEvent ev) {
    JTextPane editor = (JTextPane) ev.getSource();            
    if (! editor.isEditable()) {

      Point pt = new Point(ev.getX(), ev.getY());
      int pos = editor.viewToModel(pt);
      if (pos >= 0) {                    
        Document doc = editor.getDocument();
        if (doc instanceof DefaultStyledDocument) {
          DefaultStyledDocument hdoc = 
            (DefaultStyledDocument) doc;
          Element e = hdoc.getCharacterElement(pos);
          AttributeSet a = e.getAttributes();
          String href = (String) a.getAttribute(HTML.Attribute.HREF);
          if (href != null) {
            if (isShowToolTip()){
            if (isShowCursorFeedback()) {    
              if (getCursor() != handCursor) {
          else {
            if (isShowToolTip()){
            if (isShowCursorFeedback()) {    
              if (getCursor() != defaultCursor) {
      else {

   * Called for a mouse click event.
   * If the component is read-only (ie a browser) then 
   * the clicked event is used to drive an attempt to
   * follow the reference specified by a link.
   * @param e the mouse event
   * @see MouseListener#mouseClicked
  public void mouseClicked(MouseEvent e) {
    JTextPane editor = (JTextPane) e.getSource();
    if (! editor.isEditable()) {
      Point pt = new Point(e.getX(), e.getY());
      int pos = editor.viewToModel(pt);
      if (pos >= 0) {
        // get the element at the pos
        // check if the elemnt has the HREF
        // attribute defined
        // if so notify the HyperLinkListeners

i did not implement full program but this will help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.