This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

How to put a hypertext in JEditorPane

dkumar sharma
Greenhorn

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.
thanks
Dean Watson
Greenhorn

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
Greenhorn

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
Author
Ranch Hand

Joined: Mar 29, 2001
Posts: 88
Here's some code from the Swing tutorial at the JDC to do that.
<pre>
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
jt.setEditable(false);
// 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();
jt.setPage(url);
input.setText (url.toString());
} catch (IOException io) {
JOptionPane.showMessageDialog (
Browser.this, "Can't follow link",
"Invalid Input",
JOptionPane.ERROR_MESSAGE);
jt.setDocument (doc);
}
}
});
}
}
});
JScrollPane pane = new JScrollPane();
pane.setBorder (
BorderFactory.createLoweredBevelBorder());
pane.getViewport().add(jt);
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",
JOptionPane.ERROR_MESSAGE);
}
}
});
add (input, BorderLayout.SOUTH);
}
}
</pre>
------------------
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
 
Similar Threads
how do i print or draw a URL in a java applet
Embedding hypertext link in e-mail using java.mail
JeditorPane
Disable selection from the JEditorPane
create a html help menu