I have the below code to add html into a JEditorPane
JEditorPane content = new JEditorPane ();
content.setEditable(false);
content.setContentType( "text/html" );
content.setText(resultText);
JScrollPane bottomScrollPane = new JScrollPane(content);
bottomScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
bottomScrollPane.setBorder(BorderFactory.createTitledBorder("Swing Rendered"));
After this step, i have added the JEditorPane instance "content" into a JPanel instance and can able to see the result perfectly. But when i try to click the links shown, its not working.
How do i make these links clickable and it should redirect user to specific url in the browser?
regards, Balan
You need to add HyperlinkListener to the JEditorPane:
pane.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent r){
try{
if(r.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
pane.setPage(r.getURL());
}
catch(Exception e){
}
}
});
Or you can do anything else in the listener... for example, opening a link in the default browser:
You need to add HyperlinkListener to the JEditorPane:
pane.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent r){
try{
if(r.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
Desktop.getDesktop().browse(new URI(r.getURL().toURI()));
}
catch(Exception e){
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.