Hyperlinks in a JEditorPane

Hi,
I have an application that connects to a database and retrieves results from the database and then displays the results in a JEditorPane - the results are in the form of a hyperlink - so when i click on the link then a web page is opened up inside of the JEditorPane.
The question i have is, once i have clicked on the link how can i return to the original page, rather thn having to run the program from the start again ?
Is there a pre - defined "back"/"previous page" method i can use, to go back to the previous page ?
Any help would be much appreciated
Thanks Again

sjhori214,
When you read from the database, if you put the contents ( presumably some html with hyperlinks etc) into a String object and store it somewhere (perhaps create a new object which you have a static instance of, and load it into a String variable of that object).
Then, assuming you are using the correct editor kit (text/html), use the
setText( myObject.HTMLfromDB )type of syntax to display this HTML page.
If you also have a button (or other) somewhere in your app, whose actionListener() method calls
setText( myObject.HTMLfromDB )this would re-load the HTML back into the JEditorPane, without re-loading form the Database.
If you wanted to simply implament a "back" button, as supposed to a "home" button you could create a vector of myObject (see above) objects, and navigate back and forward in it and use the setText method to display its contents (of course you would have to add a HyperlinkListener object that fires on page moving and then use getText() to load the latest HTML into a new object.
Hope this gives you some idea of how to proceed
Guy

Similar Messages

  • Hyperlink trouble in JEditorPane

    Hi all,
    I am having real trouble with getting a simple HTML document up and running in JAVA. WHY did they make it so HARD?
    I want to be able to use internal links, e.g click here to go to top of page etc.
    Now apparently I need to add a HyperlinkListener to the JEditorPane. In the code you'll see that i've commented it out. If its left in i get
    DisplayHTML.java:61: addHyperlinkListener(javax.swing.event.HyperlinkListener) in javax.swing.JEditorPane cannot be applied to (DisplayHTML)If i change the start class to:
    public class DisplayHTML extends JFrame implements ActionListener, HyperlinkListenerI get:
    DisplayHTML.java:14: cannot resolve symbolAnd even if it did work there is still a mass of code that needs to be implemented for the internal links to work. grrrrrrrrrrrrr for a language that is meant to be excellent on the web it has poor support for HTML.
    If anyone can shed any light on this I would be grateful. Many thanks
    drb2k2
    Anyway here is the code
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.text.StyledEditorKit;
    import javax.swing.text.html.HTMLEditorKit;
    import java.net.URL;
    import java.net.MalformedURLException;
    import java.awt.event.*;
    import java.io.*;
    public class DisplayHTML extends JFrame implements ActionListener
         Color Background = new Color(100, 135, 220);
         Color Buttons     = new Color(140, 170, 230);
         Container content;
         String Keep;
         JEditorPane jed;
         public DisplayHTML()
              UIManager.put("Button.select", Buttons);     
              content = getContentPane();
              content.setBackground(Background);
              BorderLayout border = new BorderLayout();
              content.setLayout(border);
              JPanel Button_Panel = new JPanel();
              BorderLayout buttons = new BorderLayout();
              Button_Panel.setLayout(buttons);
              Button_Panel.setBackground(Background);
              JButton Save = new JButton("Save");
              Save.setBackground(Buttons);
              Save.setToolTipText("Press here to save the results file in HTML format");
              Save.addActionListener(this);
              Save.setActionCommand("Save");
              Button_Panel.add(Save, BorderLayout.WEST);
              JButton Quit = new JButton("Quit");
              Quit.setBackground(Buttons);
              Quit.setToolTipText("Press here to exit the screen and loose results");
              Quit.addActionListener(this);
              Quit.setActionCommand("Quit");
              Button_Panel.add(Quit, BorderLayout.EAST);
              content.add(Button_Panel, BorderLayout.NORTH);
              String str = new String("file:\\" + System.getProperty("user.dir") + "\\" +"Analysis.html");
              try
                   URL url =new URL(str);
                   HTMLEditorKit lhekFile = new HTMLEditorKit();
                   jed =new JEditorPane();
                   jed.setEditorKit( lhekFile );
              //     jed.addHyperlinkListener(this);
                   jed.setEditable(false);
                   JScrollPane htmlScroll = new JScrollPane(jed);
                   htmlScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                   htmlScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                   jed.setPage(url);
                   content.add(htmlScroll, BorderLayout.CENTER);
              catch(MalformedURLException e){System.out.println("can't find file");}
              catch(IOException e1){System.out.println("can't load file");}
              setSize(400,400);
              setVisible(true);
         public void actionPerformed(ActionEvent e)
    }

    whoops, forgot to include the code
    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;
    import java.net.MalformedURLException;
    import javax.swing.text.html.*;
    import java.awt.event.*;
    import java.io.*;
    public class DisplayHTML extends JFrame implements ActionListener
         Color Background = new Color(100, 135, 220);
         Color Buttons     = new Color(140, 170, 230);
         Container content;
         String Keep;
         JEditorPane jed;
         public DisplayHTML()
              UIManager.put("Button.select", Buttons);     
              content = getContentPane();
              content.setBackground(Background);
              BorderLayout border = new BorderLayout();
              content.setLayout(border);
              JPanel Button_Panel = new JPanel();
              BorderLayout buttons = new BorderLayout();
              Button_Panel.setLayout(buttons);
              Button_Panel.setBackground(Background);
              JButton Save = new JButton("Save");
              Save.setBackground(Buttons);
              Save.setToolTipText("Press here to save the results file in HTML format");
              Save.addActionListener(this);
              Save.setActionCommand("Save");
              Button_Panel.add(Save, BorderLayout.WEST);
              JButton Quit = new JButton("Quit");
              Quit.setBackground(Buttons);
              Quit.setToolTipText("Press here to exit the screen and loose results");
              Quit.addActionListener(this);
              Quit.setActionCommand("Quit");
              Button_Panel.add(Quit, BorderLayout.EAST);
              content.add(Button_Panel, BorderLayout.NORTH);
              String str = new String("file:\\" + System.getProperty("user.dir") + "\\" +"Analysis.html");
              try
                   URL url =new URL(str);
                   HTMLEditorKit lhekFile = new HTMLEditorKit();
                   jed =new JEditorPane();
                   jed.setEditorKit( lhekFile );
                   jed.setContentType("text/html");       
                   jed.addHyperlinkListener(new javax.swing.event.HyperlinkListener()
                        {public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent e)
                             JEditorPane pane = (JEditorPane) e.getSource();
                              if (e instanceof HTMLFrameHyperlinkEvent)
                                  HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
                                  HTMLDocument doc = (HTMLDocument)pane.getDocument();
                                  doc.processHTMLFrameHyperlinkEvent(evt);
                              else
                                  try
                                        pane.setPage(e.getURL());
                                  } catch (Throwable t)
                                  {t.printStackTrace();}
                   jed.setEditable(false);
                   JScrollPane htmlScroll = new JScrollPane(jed);
                   htmlScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                   htmlScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                   jed.setPage(url);
                   content.add(htmlScroll, BorderLayout.CENTER);
              catch(MalformedURLException e){System.out.println("can't find file");}
              catch(IOException e1){System.out.println("can't load file");}
              setSize(400,400);
              setVisible(true);
         public void actionPerformed(ActionEvent e)
    }

  • How to insert hyperlinks in RTF document shown in JEditorPane?

    This is a compound question so bear with me on this one :)
    What I need is to insert an hyperlink in a JEditorPane; Store it as RTF code; Retrieve it; Show it and click on it to go the the destination.
    1) So, first question, is there some method to automatically add the hyperlink and will it be saved in RTF with RTFEditorKit? (I'm actually using AdvancedRTFEditorKit but an answer to either will be enough)
    2) I tried a method that adds what appears as a link but the document that is stored doesn't have the necessary RTF codes, only the formatting that makes it look like a link. I also checked the actual RTF codes I'd need to add and they seem easy to do. So, is there a way to add the actual underlying RTF codes to the document while it's in the JEditorPane?
    I know the question(s) isn't too specific, if you can shed some light over the whole thing and point me in the right direction, it'd be great!

    Just a follow up to my own question. I found a way to answer my question 2) and I'm now storing the rtf code for hyperlinks. Now, when I show the document in JEditorPane with RTFEditorKit, I get the name of the hyperlink with no formatting indicating that it's a hyperlink.
    Example. The link "http://www.google.com" with the name "Google", only shows "Google" in plain text.
    Is it because the components I'm using don't support hyperlinks from RTF files even though they're supported from HTML files?
    Is there any way you can see this working? Is it possible to include something inside the JEditorPane that the user can click, instead of the hyperlink?

  • Hand cursor not displayed when mouse is over JEditorPane hyperlinks in 1.5

    Java version 1.5 does not show a hand cursor when the user mouses over a hyperlink within a JEditorPane, which it did in version 1.4.x.
    Create a new JEditorPane. Then set the content type (setContentType) as "text/html". Then use setText to add an html hyperlink. When the user mouses over the hyperlink, the cursor remains a pointer cursor. The cursor used to change into a hand cursor symbolizing a hyperlink.
    p.s.) The hyperlink still works, the cursor just doesn't change.
    Has anyone noticed this? Has it been reported as a bug? Is there a workaround?
    Thanks for any/all help!

    I have the same problem which I cannot seem to solve. I have a JEditorPane in a JDialog, not a JFrame, and I do not get a Hand cursor over the link. If I use a JFrame, I get the hand cursor.
    Any ideas? I tried with setting the cursor by hand using
    pane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); in the ENTERED-part of a slightly more advanced hyperlink listener (gets pane in constructor). But that did not help either.
    Here is the code. Check out the difference between JDialog and JFrame.
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.awt.*;
    public class HyperLink extends JDialog
         public static void main(String[] args)
              HyperLink h = new HyperLink();
              h.setVisible(true);
         public HyperLink()
              Container cp = getContentPane();
              JEditorPane p = new JEditorPane("text/html",
                                  "<html><body><a href='http://www.google.com'>Google</a></body></html>");
              p.addHyperlinkListener(new HyperlinkListener() {
                   public void hyperlinkUpdate(final HyperlinkEvent he) {
                       HyperlinkEvent.EventType type = he.getEventType();
                       if (type == HyperlinkEvent.EventType.ENTERED) {
                        System.err.println("entered");
                       else if (type == HyperlinkEvent.EventType.EXITED) {
                        System.err.println("Exited");
                       else if (type == HyperlinkEvent.EventType.ACTIVATED) {
                        System.err.println("Now going to " + he.getURL());
              p.setEditable(false);
              cp.add( p );
              pack();
    }

  • JEditorPane can't handle POST from HTML form?

    I posted this before but i never got an answer so i'll try again :-)
    Im running a tomcat server were i have a jsp script that uploads files to the server. What i need to do to is to use a regular java application for the rest of the "project" im working on.
    And i can get the JSP page to be displayed properly using JEditorPane but i cant get the html FORM POST in the JSP to execute... i dont know if theres some limits in JEditorPane or if im just using the wrong type of approach on this one.
    This is the server
    http://chilly-schrimp.kicks-ass.org/uploadbean/SimpleUpload.jsp
    (only works if my computer is turned on)
    Heres the code:
    =======================================================================
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    public class JSPtest {
    public static void main(String[] args) {
    JEditorPane pane = null;
    try {
    pane = new JEditorPane("http://chilly-schrimp.kicks-ass.org/uploadbean/SimpleUpload.jsp");
    catch (IOException ex) {
    ex.printStackTrace(System.err);
    System.exit(1);
    pane.setEditable(false);
    // Add a hyperlink listener.
    final JEditorPane finalPane = pane;
    pane.addHyperlinkListener(new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent ev) {
    try {
    if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
    finalPane.setPage(ev.getURL());
    } catch (IOException ex) { ex.printStackTrace(System.err); }
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new JScrollPane(finalPane));
    frame.setSize(350,400);
    frame.setVisible(true);
    =======================================================================
    any suggestions??
    thanx
    /eric

    I am using request.getParameter(passed variable name)
    to pass data from HTML form to JSP with no problem.
    WHen I try to pass data from HTML form of
    multipart/form-dta type to JSP as following:
    < form enctype="multipart/form-data" name="theForm"
    method="post"
    ACTION="http://titan.ssd.loral.com:7778/ifs/jsp-bin/if
    -cts/stringsecond.jsp">
    The passed value is null. Why?because the jsp most likely does not handling of POST parameters like this.
    How can I pass data successfully from this form to
    JSP?jsp's are not meant to read such amounts of data. this (= uploading) is a typical task for a specialized servlet. there you have full control over input and output. if you need to, you can still forward to a jsp after processing in the servlet.
    How can I pass data from JavaScript to JSP???? i'm not sure what exactly you mean. normally you put it into an url and submit it.
    robert

  • Space after hyperlink in JEditorPane

    Hi all!
    I am displaying an html document in a JEditorPane, which has some hyperlinks. Everything is wotking, except that all the hyperlinks have an extra space after the text which is underlined and is a part of the hyperlink.
    I saw this problem in the bug database but marked as closed. However I still have this problem using jdk 1.4.1.
    http://developer.java.sun.com/developer/bugParade/bugs/4303958.html
    Thanks in advance for your help!

    I'm sorry but I posted this by mistake. The topic is duplicated, but the other one has the subject corrected and has duke dollars assigned.

  • How do i set multiple line with hyperlink in JEditorPane?

    hi all,
    i have a critical question where i have a JEditorPane component which it display the hyperlink. the problem occur when the display text of the hyperlink if it is too long, i need to truncate it into multiple line like "ABCDEF" to make it "AB<br>CD<BR>F". the hyperlink work only if the display text is not truncate to multiple line, it display nothing at all if i try to truncate them into multiple line.
    anyone? thanks and regards

    At the point you wish to change to two columns, Insert > Columns and then in the Layout Inspector, select the number of columns. After the insertion point, you will also need to insert another Column Change to return the following paragraphs to single column.
    Then Select the text to be bulleted and in the Text Inspector > List tab select the type of bullets.
    Note: This creates two columns that flow together. This can be tricky if you add any text later.
    If you wish to ensure alignment, create a two column text table and remove the lines with the Graphic Inspector > Lines > No Lines option and insert your text in each column and apply the bullets as above.
    This will create a text box - you then need to ensure it moves with the text by electing "Object Moves With Text" in the Wrpa Inspector.
    Message was edited by: bwfromspring hill

  • Local hyperlink in JEditorPane

    I set a HTML page from String variable to JEditorPane. I;ve got som hyperlinks to this page like
    <A NAME="AAAAA">
    Link
    I need to handle such a link. I set editable to false and implemented hyperlink listener, but event returns null URL when calling getURL() and I do not know how to handle this links. Can anyone help ??? I want to navigate through fragment of this page.

    If you don't want to have to fool around with the HyperlinkListener, just add the normal <BASE> tag to your HTML file. The <BASE> tag has the following syntax:
    <BASE HREF="baseURL" TARGET="WindowName" >
    The TARGET portion is optional!
    Theoretically, the JEditorPane should be smart enough to look for the link from the same place where the original HTML file is loaded from but apparently there is a bug somewhere that prevents it from doing that.
    ;o)
    V.V.

  • Problem with Hyperlink  in JEditorPane!!

    hi all!
    I have some problem.
    I create html page with hyperlink to myProgram.jar and when i open this html page in JEditorPane and click on hyperlink i have the some text but my program don't run.
    How write HyperlinkListener for run the my program and setUrl for open other html pages.
    thanks!

    In the future, Swing related questions should be posted in the Swing forum.
    But, there is no need to repost the question because you can just read the JEditorPane API to find an example of how to write a HyperlinkListener.

  • Hyperlink in JEditorPane URGENT

    I have encountered a problem regarding showing the html in JEditorPane(written under JApplet), since in the html code, there are some hyperlink which link to other html page. I can't link those page. Since I am not directly read the html from a file. I am passing those html code from the data I get from cgi and directly to JEditorPane. Anyone please help me solve this problem?
    Thank you..

    If you don't want to have to fool around with the HyperlinkListener, just add the normal <BASE> tag to your HTML file. The <BASE> tag has the following syntax:
    <BASE HREF="baseURL" TARGET="WindowName" >
    The TARGET portion is optional!
    Theoretically, the JEditorPane should be smart enough to look for the link from the same place where the original HTML file is loaded from but apparently there is a bug somewhere that prevents it from doing that.
    ;o)
    V.V.

  • S�ace after hyperlink in JEditorPane

    Hi all!
    I am displaying an html document in a JEditorPane, which has some hyperlinks. Everything is wotking, except that all the hyperlinks have an extra space after the text which is underlined and is a part of the hyperlink.
    I saw this problem in the bug database but marked as closed. However I still have this problem using jdk 1.4.1.
    http://developer.java.sun.com/developer/bugParade/bugs/4303958.html
    Thanks in advance for your help!

    I'm sorry but I posted this by mistake. The topic is duplicated, but the other one has the subject corrected and has duke dollars assigned.

  • JEditorPane and Hyperlinks...

    Hi together, I become desperate...
    I've a jEditorPane on the left in my application with several rows of text. any row should be a hyperlink to a special text, which I want to display in the jtextarea on the right (something like the searchsystem in ms windows, where you see all the topics left and when you choose a topic you see the details on the left).
    does anyone have an example or can give me some inputs how i implement the hypertextListener and how to know which textrow was selected?
    thanks in advance.
    steinfresser

    yes, but it's to big for my little application. is it not possible to insert a html file in a jeditorpane, where I can listen to the hyperlink? I've found a demo version of an applet in sdk1.4/demo/jfc/swingset2, but I didn't get the context. Does anyone know what's the reason and what happen in this lines?
    try {
    URL url = ;
    // System.getProperty("user.dir") +
    // System.getProperty("file.separator");
    String path = null;
    try {
    path = "/resources/index.html";
    url = getClass().getResource(path);
    catch (Exception e) {
    System.err.println("Failed to open " + path);
    url = null;
    thanks steinfresser

  • Problem with hyperlink when JEditorPane in JDialog

    This following code works great:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    * Created by IntelliJ IDEA.
    * User: ilie
    * Date: Mar 31, 2005
    * Time: 1:21:28 PM
    * To change this template use File | Settings | File Templates.
    public class Test extends JFrame
        public Test() throws Exception
            JEditorPane pane = new JEditorPane( );
            pane.setEditable( false );
            pane.setContentType( "text/html");
            pane.setText( "<html><a href=''>a</a>");
            getContentPane().add( pane );
            pack();
            setVisible( true );
        public static void main( String args[]) throws Exception
            new Test();
    }Now... if I change the JFrame to JDialog, when I move the mouse over the links the cursor does not change anymore to a hand.
    Is this a bug? I think I do everything right.
    Thank you!

    This code here... also does not work :(
    import javax.swing.*;
    import java.awt.*;
    public class Test extends JDialog
        public Test() throws Exception
            JLabel label = new JLabel( "Boo" );
            label.setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR) ) ;
            getContentPane().add( label );
            pack();
            setVisible( true );
        public static void main( String args[]) throws Exception
            new Test();
    }

  • Local HyperLinks in JEditorPane....

    Hello,
    I am having a JEditorPane with contents as HTML file.. HTML file has many local links [ <a href=#100> </a> .... < a name=100></a> ]. EditorPane setEditable() is false. I have implemented HyperlinkListener to JEditorPane... but evt.getURL() returns "null" value when evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED. So JEditorPane.setPage(URL) is not working...
    evt.getDescription() return value as "#100". I am using JDK1.3.1. How can I solve this problem ?
    thanks,
    reji

    You have to set ID attribute instead of name for destination tag.
    Try this
    best regards
    Stas
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    import javax.swing.event.*;
    public class Test
    JFrame frame;
    JEditorPane ta;
    public static void main(String args[])
    new Test();
    public Test()
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ta = new JEditorPane();
    ta.setEditorKit(new HTMLEditorKit());
    ta.setEditable(false);
    HyperlinkListener lst=new HyperlinkListener() {
    public void hyperlinkUpdate(HyperlinkEvent e) {
    if (e.getEventType()==HyperlinkEvent.EventType.ACTIVATED) {
    String descr=e.getDescription();
    if (descr.length()>0) {
    descr=descr.substring(1);
    HTMLDocument doc=(HTMLDocument)ta.getDocument();
    Element text=doc.getElement(descr);
    ta.setCaretPosition(text.getStartOffset());
    ta.addHyperlinkListener(lst);
    ta.setText("<HTML><BODY><A HREF=#100>test</A><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><A href='' ID=100>100</A></BODY></HTML>");
    JScrollPane scroll=new JScrollPane(ta);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(scroll,BorderLayout.CENTER);
    frame.setSize(300,200);
    frame.setVisible(true);

  • Hyperlink and JEditorPane again

    Hi there,
    i know this has been handled a few times but that doesn�t helped me much...
    so i ask it again.
    i want to write a really basic HyperlinkListener, but it does not work.
    heres my code
    HyperlinkListener myListener = new HyperlinkListener()
      public void hyperlinkUpdate(HyperlinkEvent he)
        try
          if(he.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
            editorPane.setPage(he.getURL() );
        catch(IOException ioe)
          System.out.println(ioe);
          ioe.printStackTrace();
    };why does it not work?
    theres no exception, if there would be one i would see in System.out...
    any ideas?
    thx anyway
    cu Errraddicator

    HyperlinkListener works on read-only JEditorPane's.
    Look at the documentation: setEditable(false) has to be set.

Maybe you are looking for