How can I add a clickable url in a JTextPane like this
or If I ask other way, how can I make the links in JTextPane clickable ?
When the user click or double click this link the default browser will open the link that is clicked.
I have a JTextPane(EditingArea that extends JTextPane in the code) that is cabable of adding any string with any color with its append function. So I can make the links blue. But I couldn't figure how can I make the links the clickable and open it in the default browser.
I found how can I open the link in the default browser. See my second post below
Note:
I will add the links when program executing. I will make them blue and I know I am adding a URL. So there is no problem that search the text and look for URLs. Maybe this make things simpler for you and me.
import ***
***
public class TextEditor extends JScrollPane {
public static EditingArea editor;
final Clipboard clipboard;
static String copiedSelection = null;
TextEditor(){
super();
editor = new EditingArea();
this.setViewportView(editor);
this.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
editor.addMouseListener(new MouseClickHandler() );
clipboard =
getToolkit().getSystemClipboard();
}
public class EditingArea extends JTextPane {
public void append(Color c, String s) { // better implementation--uses
// StyleContext
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
StyleConstants.Foreground, c);
int len = getDocument().getLength(); // same value as
// getText().length();
setCaretPosition(len); // place caret at the end (with no selection)
setCharacterAttributes(aset, false);
replaceSelection(s); // there is no selection, so inserts at caret
}
}
}