Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to put a hypertext in JEditorPane

 
dkumar sharma
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you get any response to your question? I'm trying to figure the same thing also
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also looking for the same thing.
 
Kris Schneider
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just post a response (including code ) about this in Gary Bryan's question "html text in JTextPane"
:-)
 
John Zukowski
Author
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(....).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic