opening hyperlinks in a JEditorPane

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dunkymunky
    New Member
    • Mar 2008
    • 3

    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:

    Code:
    public interface HyperLinkListener {
      public void hyperlinkUpdate(HyperlinkEvent e);
    } 
    public void hyperlinkUpdate(HyperlinkEvent evt) {
        
        if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          try {
            LinksList.setPage("http://www.facebook.com");        
          }
          catch (Exception e) {        
          } 
        }
    }
     
    	private void links(){
    		
    					LinksList.addHyperlinkListener(this);
    		String[] linksarray = new String[10];
    		int i = 0;
    		LinksList.setContentType("text/html");
    		
    		try{	
    		BufferedReader links = new BufferedReader (new FileReader("links.txt"));
    		
    		while(i<10){
    			linksarray[i] = links.readLine();
    			i++;
    			
    		}//end while
    		
    		i=0;		
    		while(linksarray[i]!=null){
    			LinksList.setText(linksarray[i]+"\n");
     
     
    			i++;
    		}//end while
    	}//end try
    	
    	catch (FileNotFoundException e){
    	errorMessageDialogue(e.getMessage());
    	}//end catch
    	
    	catch (IOException e){
    	errorMessageDialogue(e.getMessage());
    	}//end catch
    		
     
    	}//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...c an anyone suggest what the code might be?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Why are you defining your own HyperLinkListen er interface? It won't be called
    by your JTextPane. Use the one supplied by the core classes. Once you've
    got the HTTPHyperLinkEv ent you can get the URI that was clicked. Give that
    URI to the Desktop.browse( ) method and you're done.

    kind regards,

    Jos

    Comment

    • dunkymunky
      New Member
      • Mar 2008
      • 3

      #3
      hmm...thanks for the reply...but still not sure how to go about it...?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dunkymunky
        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

        Comment

        • dunkymunky
          New Member
          • Mar 2008
          • 3

          #5
          i've gone all over the API, and looked at other examples on the internet, but just cant seem to get it working properly...boil er plate code would be much helpful (im assuming boiler plate code is basic, skeleton code :P )

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dunkymunky
            i've gone all over the API, and looked at other examples on the internet, but just cant seem to get it working properly...boil er 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 HyperLinkListen er implementation to the
            JEditorPane (don't define your owen HyperLinkListen er 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

            Comment

            Working...