wood burning stoves 2.0
The moose likes Swing / AWT / SWT and the fly likes How to put a hypertext in JEditorPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to put a hypertext in JEditorPane" Watch "How to put a hypertext in JEditorPane" New topic

How to put a hypertext in JEditorPane

dkumar sharma

Joined: May 10, 2001
Posts: 17
hi all,
i need to implement series of hypertext in JEditorPane , if i click on these text it should take me to a particular site. please help out if any one.
Dean Watson

Joined: Apr 30, 2001
Posts: 6
Did you get any response to your question? I'm trying to figure the same thing also
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
I am also looking for the same thing.
Kris Schneider
Ranch Hand

Joined: Feb 14, 2001
Posts: 71
Actually, the API docs for JEditorPane explain what you need to do. Make sure to check out the example code of a HyperlinkListener in the class description . This is the interface used for getting notifications that something has happened (activated, entered, or exited) with respect to a hyperlink.
Ana Mihailescu

Joined: Apr 26, 2001
Posts: 29
I just post a response (including code ) about this in Gary Bryan's question "html text in JTextPane"
John Zukowski
Ranch Hand

Joined: Mar 29, 2001
Posts: 88
Here's some code from the Swing tutorial at the JDC to do that.
public class Browser extends JPanel {
Browser() {
setLayout (new BorderLayout (5, 5));
final JEditorPane jt = new JEditorPane();
final JTextField input =
new JTextField("http://java.sun.com");
// make read-only
// follow links
jt.addHyperlinkListener(new HyperlinkListener () {
public void hyperlinkUpdate(
final HyperlinkEvent e) {
if (e.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Save original
Document doc = jt.getDocument();
try {
URL url = e.getURL();
input.setText (url.toString());
} catch (IOException io) {
JOptionPane.showMessageDialog (
Browser.this, "Can't follow link",
"Invalid Input",
jt.setDocument (doc);
JScrollPane pane = new JScrollPane();
pane.setBorder (
add(pane, BorderLayout.CENTER);
input.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
try {
jt.setPage (input.getText());
} catch (IOException ex) {
JOptionPane.showMessageDialog (
Browser.this, "Invalid URL",
"Invalid Input",
add (input, BorderLayout.SOUTH);
John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"
[This message has been edited by John Zukowski (edited May 15, 2001).]
[This message has been edited by John Zukowski (edited May 15, 2001).]

John Zukowski Author of <a href="http://www.amazon.com/exec/obidos/ASIN/189311578X/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">"Definitive Guide to Swing for Java 2"</a>,<br /><a href="http://www.amazon.com/exec/obidos/ASIN/1893115925/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Java Collections"</a> and <a href="http://www.amazon.com/exec/obidos/ASIN/1893115984/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Learn Java with JBuilder 6"</a>
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Hi friends.
I use your code but it can't work.When I click it whick don't open a IE and go to that website. What's wrong?

When I face C# and Java I choose Java.
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100
this code is not meant to open IE, or any other browser. This code creates a browser in which the link should be followed in the JEditorPane. You just created a browser. Thin as thin gets, but never-the-less, a browser indeed!

An egg is a chicken's house!
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Yes . You are right.
It open a webpage in it other than open an IE.
If I want to open a new explore I must use Runtime.getRuntime().exec(....).
I agree. Here's the link: http://aspose.com/file-tools
subject: How to put a hypertext in JEditorPane
jQuery in Action, 3rd edition