Inserting HTML into JEditorPane

I have a window with a JEditorPane on top and a JTextField below it. I need to be able to type raw HTML into the edit field and have it appended to the HTML document displayed above.
For example if I type...
This is <b>bold
I need to append that to the document. However I want the style to stick. Notice I did not close the tag above. I want the next line I enter into the edit field...
and so is this but not</b> this.
I want this to appear bold up until the closing tag and then return to normal. The problem is that every time I append new text to the document the style is restored to normal.
I insert the text with the following code. I send the contents of the JTextField to the following function when I hit return.
void processInput(String input) {
try {
HTMLDocument doc = (HTMLDocument) output.getDocument(); // output is my JEditorPane
editor.insertHTML(doc, doc.getLength(), input, 0, 0, HTML.Tag.BODY); // editor is HTMLEditorKit
// relevant catch clauses
Basically I want the two broken input sequences to appear in the editor as one seamless entry. I want to be able to type these two entries and have them show up as if they were entered as one entry.
entry 1:This is <b>bold
entry 2: and so is this but not</b> this.
I want to somehow cause this to be displayed as if I typed...
This is <b>bold and so is this but not</b> this.
Is there a way I can do this?
Thanks.
-Russ

as i understand you you want to write
entry 1:This is <b>bold
entry 2: and so is this but not</b> this.
in two lines
Try this
entry 1:This is <b>bold<br>
entry 2: and so is this but not</b> this.

Similar Messages

  • Problem to display Animated Gif from HTML into JEditorPane

    I have a problem displaying animated gif that comes from URL (HTML) into JEditorPane.
    Let me show you the source I have:
    * @author Dobromir Gospodinov
    * @version 1.0
    * Date: Dec 6, 2002
    * Time: 6:47:53 PM
    package test.advertserver;
    import javax.swing.*;
    import java.awt.*;
    import java.io.IOException;
    public class Test {
         public static void main(String[] args) {
              JFrame frame = new JFrame();
              JEditorPane ed = new JEditorPane();
              try {
                   ed.setPage("http://localhost:8200/servlet?key=value");
              } catch (IOException e) {
                   e.printStackTrace();
              JPanel panel = new JPanel();
              panel.setPreferredSize(new Dimension(500, 500));
              panel.add(ed);
              frame.getContentPane().add(panel);
              frame.pack();
              frame.setVisible(true);
    }Part of the returned from servlet HTML includes an img tag:
    <img src="/images/MyAnimatedGif.gif" alt="animated gif comment" width="480" height="50"  border="0">Let us assume that MyAnimatedGif.gif has 10 frames and gif is looped - when the 10th is dipslayed it has to display the 1st and so on.
    JEditorPane displays frames from 1 to 10 correctly but does not start from the first again. Instead JEditorPane displays a broken image.
    I locate where the problem arise:
    JEditorPane has an HTMLEditorKit that creates javax.swing.text.html.ImageView instance for every IMG tag.
    And here is the problem:
    ImageView has an ImageObserver necessary for the asynchronous image download. ImageObserver has the imageUpdate method. But this imageUpdate method is never called with ALLBITS flag raised up. Instead, after the last frame of MyAnimatedGif.gif is downloaded the imageUpdate method is called with flag ERROR raised up. Obviously this is a bug of Sun's implementation. Finaly the flag ALLBITS has to be received for normal end of image observing. But ALLBITS flag does not come.
    So, can anybody help me how to load an animated gif within JEditorPane completely.
    Thank You in advance,
    Dobromir Gospodinov
    P.S. If somebody of you wants to debbug what happens within ImageView will have to implement it (and related classes too, because of the limited package visability) borrowing the source from Sun's ImageView.

    I'm also having this problem with java 1.4.1 I discovered that some animated gifs work fine, while others stop animating. Running with java 1.3.1 fixed the problem. I'm going to report this as a bug
    Here's my code:
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    public class AnimatedGifTester
    extends JFrame
    public static void main(String argv[])
    throws Exception
    new AnimatedGifTester();
    public AnimatedGifTester()
    throws Exception
    String[] images = new String[] {
    "http://www.gif.com/ImageGallery/Animated/Animals/Photographic/dog_running.gif",
    "http://www.gif.com/ImageGallery/Animated/Characters/Cartoon/java.gif",
    "http://www.webdeveloper.com/animations/bnifiles/anielg.gif",
    "http://www.webdeveloper.com/animations/bnifiles/cat2.gif",
    "http://images.animfactory.com/animations/animals/fish/big_fish_swimming_md_wht.gif",
    "http://www.webgenies.co.uk/images/martian.gif",
    "http://www.webdeveloper.com/animations/bnifiles/at_sign_rotating.gif",
    "http://www.webdeveloper.com/animations/bnifiles/arrow_1.gif",
    "http://www.gif.com/ImageGallery/Animated/Characters/Cartoon/javaacro.gif",
    "http://java.sun.com/products/java-media/2D/samples/suite/Image/duke.running.gif",
    "http://www.gif.com/ImageGallery/Animated/SouthPark/Cartoon/stan.gif"
    StringBuffer buffer = new StringBuffer("<html><body>");
    for (int idx = 0; idx < images.length; idx++)
    buffer.append("<img src='" + images[idx] + "'>");
    buffer.append("</body></html>");
    String html = buffer.toString();
    // save a copy of the html to open in a browser so we can see what it's
    // supposed to look like
    BufferedWriter writer = new BufferedWriter(new FileWriter("animatedGifTest.html"));
    writer.write(html);
    writer.close();
    JEditorPane editorPane = new JEditorPane("text/html", html);
    editorPane.setEditable(false);
    getContentPane().add(editorPane);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(new Dimension(400, 600));
    show();

  • Inserting HTML into JTextPane

    Hi,
    I am trying to insert HTML into JTextPane.
    I am using the following code for the same.
    JTextPane jedit = new JTextPane();       
    jedit.setContentType("text/html");
            HTMLDocument doc = (HTMLDocument)jedit.getDocument();
            String text = "<a href=\"???\">hyperlink</a>asd<a href=\"???\">hyperlink123</a>";
            HTMLEditorKit editorKit = (HTMLEditorKit)jedit.getEditorKit();
            editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);
            doc.insertString(doc.getLength(),"Hi testing",null);
            text = "<a href=\"???\">hyperlink123</a>";The problem is that the HTML gets inserted into new line. I do not want the new line.
    I know there is an API like insertBeforeEnd(...) but do not know how to use that.
    Any help for the above problem will be of great use.
    Thanks.

    look I have got the answer ... I guess this would be the root cause of the problem of new line.
    when ever you want the text to be inserted
    -at new line provide the last argument  of HTMLDocument.insertHTML as null
    - at same line provide the last argument as the HTML tag you are inserting into the JtextPane's document.thats it !!!
    ENJOY :-)

  • Insert html into styled document

    Hi,
    I would like to insert html content into a JEditorPane that uses a StyledDocument and DefaultStyledEditorKit. I do not want to use the HtmlEditorKit for some reasons though that might change later. Now, I want to insert ElementSpec objects into my styled document and the only way seems to be doc.insert(o, espec) where espec is of type ElementSpec[]. I would like to insert my ElementSpecs one by one if possible.
    Can someone tell me how I can do this.
    Cheers,
    Omprakash.V

    Hi,
    I was referring to DefaultEditorKit in the above post when I said DefaultStyledEditorKit.
    Cheers,
    Omprakash.V

  • Inserting HTML into a table?

    I am trying to find an easier method of inserting html code into a column of type varchar2. I have to insert 100's of lines of html into a table weekly and the inserts keep failing because of special charecters within the html code itself. I have written scripts using UTL_FILE functions but this is very tedious to do weekly. Is there a better method to accomplish inserting html?

    try this.
    Hi,
    procedure start_import (p_filename in varchar2)
    is
    v_lob clob;
    v_offset number := 1;
    v_file bfile := bfilename('IMPORT',p_filename);
    v_lob_length number;
    begin
    -- File open
    dbms_lob.fileopen(v_file);
    -- get file length
    v_lob_length := dbms_lob.getlength(v_file);
    -- Create temp LOB (CACHED)
    dbms_lob.createtemporary(v_lob,TRUE);
    -- fill Temp Lob with filecontent
    dbms_lob.loadfromfile(v_lob,v_file,v_lob_length);
    -- Close file
    dbms_lob.fileclose(v_file);
    So, now you have the Filecontent in your temp LOB.
    For more info. about LOB, read Oracle Documentation.
    Hope this helps,
    Kalpen
    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by Troy Johnson ([email protected]):
    I am trying to find an easier method of inserting html code into a column of type varchar2. I have to insert 100's of lines of html into a table weekly and the inserts keep failing because of special charecters within the html code itself. I have written scripts using UTL_FILE functions but this is very tedious to do weekly. Is there a better method to accomplish inserting html?<HR></BLOCKQUOTE>
    null

  • Inserting html into a composition in muse

    Hi, I am trying to insert some html into a composition in Muse.  I would like to have a clickable button trigger, and then a kind of overlay opens up with an interactive calculator inside.  I have the html for the calculator, the problem is no matter how I try to insert it into Muse, the calculator either disappears (as well as the trigger) for some reason, or the calculator stays on the page the entire time, losing the whole clickable effect.
    Can someone tell me the best way to do this?

    Hello,
    Which Composition are you trying to Use ? Blank,Featured News, Lightbox Display, Presentation or Tooltip ?
    Also Make Sure to drag and drop the inserted HTML window over the target (Border of target gets activated once you move it over), Hopefully this will work.
    I created the same using Tooltip composition and its working fine. Please take a look at the link.
    Home
    If it do not work then please share the calculator HTML  code that you are inserting so that I can do some test at my end.
    Regards
    Vivek

  • Insert sentences into JEditorPane

    Hi,
    I am trying to insert a user input sentence into JEditorPane. Before that, the user will type a word which is search for in the JEditorPane. when the word is found in a sentence, the user's input sentence is inserted to be the line juz after the line with the keyword.
    from http://forum.java.sun.com/thread.jspathreadID=650821&messageID=3827757
    i learnt how to find the line which contains the keyword, but how do i push the subsequent lines downwards, and insert the user's sentence in between them??
    Regards,
    yiming

    I am on a roll this morning.
    I am still pulling my hair out with the kit but here is what I did.
    First get the editor kit. If you traverse the code there are a lot of actions that are built it.
    I grabbed these actions in the following manner.
    HTMLEditorKit kit = new HTMLEditorKit();
    Map _editActionMap;
    Action[] action = kit.getActions();
    for ( int iIndex = 0; iIndex < action.length; iIndex ++ ) {
    // if you want to see the action name.
    System.out.println( action.toString());
    // Build a convient hash of the actions by name.
    _editActionMap.put( action[iIndex].getValue( Action.NAME ), action[iIndex] );
    Now you have a map of all the built in actions. Guess what insert table is in the list. Do see the rest just iterator through the list and print them out.
    Now you have the actions.
    Add the action for insert table to a pop up menu.
    tableMenu is a JPopmenu menu.
    JMenuItem menu;
    menu = tableMenu.add( (Action)_editActionMap.get( "InsertTable" ));
    menu.setText( "Insert Table" );
    The default label is the actions name. So I simply rename the pop menu title.
    Cut, copy, paste, bold, italic are also in there.
    Regards
    Carl

  • Trouble inserting text into JEditorPane

    Hello.
    I am trying to use a URL Connection and the read method to produce an HTML page in a JEditorPane.
         purlRec is a data record that contains information relating to the URL in question.
         pis is an input stream create from the URLConnection.getInputStream() method
    private void loadFromURL(UrlRecord purlRec, InputStream pis ) {
    URL url;
    String tmpStr = (String)purlRec.getUrlName();
    String baseUrl = (String)purlRec.getUrlName();
    setContentType("text/html");
    try {
    HTMLDocument doc = (HTMLDocument)getDocument();
    doc.setBase(new URL(baseUrl));
    this.read(is,doc);
    repaint();
    catch (Exception e) {
    System.err.println("Couldn't create URL: " + tmpStr);
    System.out.println("error:"+e.getMessage());
    I get the following when I execute the code. Am I not using this correctly or is there something I am missing? I am using the read from input stream so that I can connnect to url that require authentication. I know that the URL connection works because I am able to write the output of the stream to a text file in a test function.
    Couldn't create URL: http://www.google.com
    java.lang.RuntimeException: Must insert new content into body element-
         at javax.swing.text.html.HTMLDocument$HTMLReader.generateEndsSpecsForMidInsert(HTMLDocument.java:1878)
         at javax.swing.text.html.HTMLDocument$HTMLReader.<init>(HTMLDocument.java:1854)
         at javax.swing.text.html.HTMLDocument$HTMLReader.<init>(HTMLDocument.java:1729)
         at javax.swing.text.html.HTMLDocument$HTMLReader.<init>(HTMLDocument.java:1724)
         at javax.swing.text.html.HTMLDocument.getReader(HTMLDocument.java:125)
         at javax.swing.text.html.HTMLEditorKit.read(HTMLEditorKit.java:228)
         at javax.swing.JEditorPane.read(JEditorPane.java:504)
         at javax.swing.JEditorPane.read(JEditorPane.java:478)
         at com.UrlChecker.panels.HTML.EditorPane._$34246(EditorPane.java:105)
         at com.UrlChecker.panels.HTML.EditorPane.changeUrls(EditorPane.java:142)
         at com.UrlChecker.demo.UrlChecker.changeHtml(UrlChecker.java:131)
         at com.UrlChecker.panels.Report.URLTabView.mouseClicked(URLTabView.java:91)
         at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:208)
         at java.awt.Component.processMouseEvent(Component.java:5096)
         at java.awt.Component.processEvent(Component.java:4890)
         at java.awt.Container.processEvent(Container.java:1566)
         at java.awt.Component.dispatchEventImpl(Component.java:3598)
         at java.awt.Container.dispatchEventImpl(Container.java:1623)
         at java.awt.Component.dispatchEvent(Component.java:3439)
         at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
         at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3174)
         at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
         at java.awt.Container.dispatchEventImpl(Container.java:1609)
         at java.awt.Window.dispatchEventImpl(Window.java:1585)
         at java.awt.Component.dispatchEvent(Component.java:3439)
         at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
         at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
         at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
    error:Must insert new content into body element-
    Thank you

    I tried using this, but I get an UNAUTHORIZED Status code (401) indicating request requires HTTP authentication. ( HttpServletResponse error code ). This is why the encoded authentication is used. It works when I try to just read from the url using an InputStream but not when I try passing the same InputStream to the JEditorPane.
    Any Idea of how to use JEditorPane.read(InputStream in , Object desc) or why it would work in one instance and not in the other?
    Thanks

  • Inserting HTML into a web page

    On a previous thread I asked how to insert ones own code into a page created in iWeb.Some kind person told me how to do it,namely: find the html page in iMac on my hard drive,in TextEdit insert the code that I want and click Save.It all worked fine. However when I go back into my web page in iWeb and make a change followed by upload, the code that I inserted in TextEditor is wiped.Is there any way that I can insert my own code into a web page and not get it wiped when I make other changes in iWeb and upload to the server? Thanks in advance

    Nope, this is how it works. If you make a change to a page like this post-publishing, and later re-publish, you have to make the html changes again! Set up a Find/Replace command in MassReplaceIt and you can do this very quickly. You can save your queries so you can make all of your changes with one click the next time you save.
    Download here:
    http://www.hexmonkeysoftware.com

  • Inserting HTML in JEditorPane

    Extremely frustrating trying to work with the HTML editing model (JEditorPane - content type text/html). I am using 1.4.2_06. Tying some basic actions to buttons or keystrokes is no problem (eg bold, italic etc.) but the most NB things like being able to drop a <br> in where the user presses enter doesn't seem possible. Has anyone found a way to drop in HTML at the caret? Would hugely appreciate any ideas.
    Matthew

    Ok, figured it out myself:
            notesArea.getInputMap().put(KeyStroke.getKeyStroke("enter"), "enterAction");
            notesArea.getActionMap().put("enterAction", new HTMLEditorKit.InsertHTMLTextAction("InsertBR", "<br>", null, HTML.Tag.IMPLIED, null, null));

  • Insert HTML into HTMLDocument

    hello
    i' have a problem using JTextPane. I want to select some text, and add some HTML tags around. For example, "Jeremie" is written ... i select all the text, i press the button and i want
    that the HTML code generated is "<font class="toto">Jeremie</font>"
    please help
    jeremie

    Yeah.. but. Where do you want the source code to be displayed ? Do you have a source code pane or something like that, or you just want HTML attributes to be applied to you text ?
    Il give you some random examples of HTML manipulation.. but I don't clearly see what you want to do :
    //set the appropriate EditorKit for JTextPane by determining its content type
    JTextPane tpa = new JTextPane();
    tpa.setContentType("text/html");
    //get a reference to the tpaDefault document
    HTMLDocument htmDoc = (HTMLDocument) tpa.getDocument();
    //get a referene to HTMLEditorKit
    HTLMEditorKit htmKit = (HTMLEditorKit) tpa.getEditorKit();
    //Get selected text from JTextPane
    String strSelText = JTextPane.getSelectedText();
    //Get selection start and end positions
    int intStartPos = JTextPane.getSelectionStart();
    int intEndPos = JTextPane.getSelectionEnd();
    //Calculate selection length
    int intSelLength = intEndPos - intStartPos;
    //Remove selection from HTMLDocument
    htmDoc.remove(intStartPos, intSelLength);
    //HTML String to insert
    String strToInsert = "<font face="Verdana" size="3" color="red">" + strSelText + "</font>";
    //Insert new formated HTML content using HTMLEditorKit
    htmKit.insertHTML(htmDoc,strToInsert,intStartPos, 0,0, HTML.Tag.FONT);  That's more or less how it works.
    Hope it helps,
    Diego

  • Inserted HTML does NOT push content when resized

    Hi,
    we are building a website for a hotel and are bound to include their channel manager module into the site. In order to do that, you have to follow these steps:
    1. Paste JavaScript code into the head section to load the module.
    2. Place a short line of code with "insert HTML" into the site where the module should appear and that's it.
    The module is generated by a server-side script from the channel manager's server and pasted into an iFrame that is also generated by this very script. The thing is: It resizes the height of the iFrame depending on the displayed content in the iFrame. So we start with 301 px height when we call the page and start the search for free rooms. The results are displayed on a 1400 px iFrame, which resizes properly when showing the results.
    THE PROBLEM: The footers stays put and the iFrame runs behind it and off the display port. There is also no scrollbar showing up allowing me to scroll to the bottom of the iFrame.
    We got three layers:
    3. Top Layer (Containing top navigation and footer, both defined in a master page that is applied to the content page)
    2. Content Layer (Containing the inserted HTML with the generated iFrame)
    1. Background Layer (Well, it contains background elements)
    WE NEED: The footer to be pushed by the ever resizing iFrame and get a scrollbar when neccessary due to iFrame height.
    What are we doing wrong?

    Thank you for your suggestions, although I don't think they will fix the problem.
    We cannot alter the server script in any way, since it is located on the channel manager's server and is just called with a customer id to show our client's hotel related data. They resize the iFrame using an inline height style tag. This causes the page to not recognize the size change and not even showing a scrollbar at all, when the iFrame exceeds the view port.
    We already put a div around the JS-call which expands with the iFrame as expected.
    <div id="u1218"><!-- custom html -->
    <div>
    <script language="javascript" type="text/javascript">
    function();
    </script>
    </div>
    </div>
    This results in an iFrame inserted after the script tag, the div around the script expands correctly. We already removed any master page objects, created a blank new master page to define header and footer areas and pasted our menu and footer directly into the page of question on the same layer as the inserted html box went. So for our understanding the expanding div around the script should push the footer on this page to whereever it ends. It just doesn't. Also we do not get any scrollbar, although the iFrame clearly gets larger than the viewport area and the page just doesn't recognize it.

  • Ability to insert HTML code snippets not available

    Supposedly I can insert HTML into a file in Contribute but
    that option is grayed-out and can't be used. Does anyone know why
    that capability would not be functioning. I have the most recent
    version of Contribute and use it on a PC

    In the main menu go to:
    1. Edit, Administer Websites
    2. Users and Roles, click Edit Role Settings
    3. go to Editing
    4. check 'Allow HTML snippets insertion...' (last option)

  • Text disappears to the right and below inserted html, any ideas?

    When I insert html into the page, the text I have to the right and below disappear from the page. If I move the html below both sets of text everything appears fine. Any ideas?

    Hello,
    Could you please provide me the link to your website (if you have uploaded it).
    Else could you please paste the html code (that you are placing on the page) here so that we can have a look at it.
    Regards,
    Sachin

  • IChat inserts HTML for ICQ users

    When I chat with other ICQ users, iChat inserts HTML into my dialogue containing formatting for the text. This only happens when the other person doesn't have iChat but a different chat client.
    Is there an option to turn this kind of unhelpful text styling off for my outgoing messages?
    iBook G4   Mac OS X (10.4.5)  

    Hi Simon,
    As far as I can tell iChat sends the HTML stuff to produce the Bubble inforamtion at the other end.
    Obviously AIM ignores it and ICQ does not.
    I am not sure if switching to Text only actually completely solves it completely but it is an option.
    There is no halfway setting.
    Thanks for the points.
    10:45 PM Sunday; March 19, 2006

Maybe you are looking for