473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

opening hyperlinks in a JEditorPane

I've got a JEditorPane that is correctly displaying HTML hyperlinks, but all it does is display them, how can I make it so that when someone clicks on this hyperlink, it opens up their default web browser and opens the correct page?

This is the code I have so far:

Expand|Select|Wrap|Line Numbers
  1. public interface HyperLinkListener {
  2.   public void hyperlinkUpdate(HyperlinkEvent e);
  3. public void hyperlinkUpdate(HyperlinkEvent evt) {
  4.  
  5.     if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  6.       try {
  7.         LinksList.setPage("http://www.facebook.com");        
  8.       }
  9.       catch (Exception e) {        
  10.       } 
  11.     }
  12. }
  13.  
  14.     private void links(){
  15.  
  16.                     LinksList.addHyperlinkListener(this);
  17.         String[] linksarray = new String[10];
  18.         int i = 0;
  19.         LinksList.setContentType("text/html");
  20.  
  21.         try{    
  22.         BufferedReader links = new BufferedReader (new FileReader("links.txt"));
  23.  
  24.         while(i<10){
  25.             linksarray[i] = links.readLine();
  26.             i++;
  27.  
  28.         }//end while
  29.  
  30.         i=0;        
  31.         while(linksarray[i]!=null){
  32.             LinksList.setText(linksarray[i]+"\n");
  33.  
  34.  
  35.             i++;
  36.         }//end while
  37.     }//end try
  38.  
  39.     catch (FileNotFoundException e){
  40.     errorMessageDialogue(e.getMessage());
  41.     }//end catch
  42.  
  43.     catch (IOException e){
  44.     errorMessageDialogue(e.getMessage());
  45.     }//end catch
  46.  
  47.  
  48.     }//end links
The JEditorPane is called LinksList, it reads HTML code from a file called links.txt and then displays it, with the standard blue colour and underlining...can anyone suggest what the code might be?
Mar 25 '08 #1
5 3158
JosAH
11,448 Expert 8TB
Why are you defining your own HyperLinkListener interface? It won't be called
by your JTextPane. Use the one supplied by the core classes. Once you've
got the HTTPHyperLinkEvent you can get the URI that was clicked. Give that
URI to the Desktop.browse() method and you're done.

kind regards,

Jos
Mar 25 '08 #2
hmm...thanks for the reply...but still not sure how to go about it...?
Mar 26 '08 #3
JosAH
11,448 Expert 8TB
hmm...thanks for the reply...but still not sure how to go about it...?
Did you read the API documents w.r.t. the relevant classes or did you expect
boiler plate code?

kind regards,

Jos
Mar 26 '08 #4
i've gone all over the API, and looked at other examples on the internet, but just cant seem to get it working properly...boiler plate code would be much helpful (im assuming boiler plate code is basic, skeleton code :P )
Mar 26 '08 #5
JosAH
11,448 Expert 8TB
i've gone all over the API, and looked at other examples on the internet, but just cant seem to get it working properly...boiler plate code would be much helpful (im assuming boiler plate code is basic, skeleton code :P )
Ok, again: the JEditorPane fires an event when a hyperlink is clicked. Your
hyperlink listeners receive that event. The event captures the URI that has been
clicked on. A Desktop object can fire the system's default browser given a
URI.

So all you have to do is register a HyperLinkListener implementation to the
JEditorPane (don't define your owen HyperLinkListener interface because that
won't work). When it receives a HyperLinkEvent it grabs the URI from it and
passes it to the Desktop class.

kind regards,

Jos
Mar 26 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: T.G. | last post by:
Hello! I'd like to ask a question about if it is possible to open a browser from a java program. I'm working with JBuilder X foundation. I have a frame or dialog - about box - and I would...
0
by: Ahmet Aksoy | last post by:
Hi, I have a JEditorPane. It is filled with some data read from somewhere. While adding new lines, JEditorPane scrolls up. At the end of the action, the last line is at the focus. But, I want the...
8
by: ajay | last post by:
How to make a new page appear in tab instead of new window. I use Netscape 7+. Is there any provision to do in HTML language ? Ajay
2
by: ChrisR | last post by:
Hi folkes I have a simple html file with 2 frames, as my Help file to my DB. It consists of 3 parts: Help.htm = Is the main file with the 2 frames. Index.htm = Is the left frame as a index...
5
by: Shawn Hogan | last post by:
Hi, I have a 3rd party control(infragistics) that seems to alter the VS.NET IDE properties grid so that there are three areas instead of two areas in the VS.NET IDE properties grid. There is a...
1
by: Bernie Hunt | last post by:
I have a JEditorPane that I'm filling with a webpage using JEditorPane.setPage(www.something.com) and it is working fine. My problem is I want to save the HTML from the page displayed into a...
3
by: aryan24 | last post by:
Hi everybod .. as part of my project , i need to call an xml file into an Jeditorpane. I have a button called Load ,when i click on it ,,i need to load the xml file into the JeditorPane...Can any1...
1
by: ksr04 | last post by:
Hello Everybody, can anyone tell how to make JEditorPane support CSS??? thanx in advance...
1
jhardman
by: jhardman | last post by:
I'm using a JEditorPane to display an HTML-formatted page. When I make changes to the page, I find I can't see the changes unless I compile a new java file and point to a new HTML file, as if the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.