Inserting text into JTextPane

I have a requirement where I need to insert some text into a JTextPane within its DocumentListener. I read somewhere that this is not a good practice. What would be the right way of doing this?
Basically, I'm listening to what user is typing and helping him by inserting some text automatically based on user choice.
Please provide code samples.

Hmmm - not knowing anything about the document listener - maybe they were talking about some kind of loop, where you insert text and then it triggers the listener to insert text which triggers the listener to insert text.... etc.
Maybe there are some other user input listeners, such as the keyListeners which might be better?
I haven't even had a look at the JTextPane API at the moment, so I don't know what I'm talking about.
Cheers,
Rene

Similar Messages

  • 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 :-)

  • A Simple Question... inserting image into JTextPane...

    Well my problem is very simple (but not for me). Before I explain it, please take a look at a simple class...
    public class Window extends JTextPane {
        Window() {
            super();
        }// Window
        public void appendText(String s,Color col) throws BadLocationException {
            StyledDocument sd = getStyledDocument();
            SimpleAttributeSet attr = new SimpleAttributeSet();
            StyleConstants.setForeground(attr,col);
            sd.insertString(sd.getLength(),s,attr);
          *     THE POINT - what should I write here, please consult description followed by the class.
        } //appendText
    } //classThe appendText method simply appends the text in text pane with desired color. Now I want to know what should I write at point THE POINT so that at end of appended string an icon (suppose end.gif) is added and is displayed in text pane. Simple?
    Stay happy,
    fadee

    Dear Fadee,
    here is a small programe for inserting images into JTextPane;
    find this comment and start
    /*All Code In this Button Action*/if you need any thing feel free to tell me,
    i'm with you brother, i'll do my best :O)
    -Best regards
    mnmmm
    * Fadee.java
    * Created on June 7, 2002, 1:13 AM
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.color.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    * @author  Brother Mohammad, Cairo, Egypt.
    * @version
    public class Fadee extends javax.swing.JFrame {
        JTextPane   tp;
        JButton     b;
        StyledDocument sd;
        SimpleAttributeSet attr;
        public Fadee() throws BadLocationException {
            b = new JButton("Press");
            tp = new JTextPane();
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    /*All Code In this Button Action*/
                    try {
                        sd = tp.getStyledDocument();
                        attr = new SimpleAttributeSet();
                        /*get default style*/
                        Style def = StyleContext.getDefaultStyleContext().
                                            getStyle(StyleContext.DEFAULT_STYLE);
                        /*add style for text default style*/
                        Style regular = tp.addStyle("regular", def);
                        StyleConstants.setFontFamily(def, "SansSerif");
                        StyleConstants.setAlignment(def, StyleConstants.ALIGN_CENTER);
                        StyleConstants.setForeground(def, Color.RED);
                        /*add style for icon create as many as icons you have
                         and don't forget to resize the photo at small size
                         to fit with font size*/
                        Style s = tp.addStyle("icon", regular);
                        StyleConstants.setAlignment(s, StyleConstants.ALIGN_JUSTIFIED);
                        StyleConstants.setIcon(s, new ImageIcon("d:\\My Photo.GIF"));
                        /*here is what user will see*/
                        sd.insertString(sd.getLength(), "Allah Akbar ", tp.getStyle("regular"));
                        sd.insertString(sd.getLength(), " ", tp.getStyle("icon"));
                    } catch (BadLocationException x) {
                        x.printStackTrace();
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(b, BorderLayout.NORTH);
            getContentPane().add(tp, BorderLayout.CENTER);
            setSize(500, 500);
            setVisible(true);
        public static void main(String args[]) {
            try {
                Fadee f = new Fadee();
            } catch (BadLocationException x) {
                x.printStackTrace();
    }

  • Inserting text into a field at the current cursor position

    Does anyone know how to insert text into a field at the current cursor position? I would like it to work similar to the Syntax Palette in Forms. I cannot figure out how to retrieve the current cursor position in order to manipulate the text.
    Any help would be great.
    Thanks.

    Hi,
    If the button and the textfield are on in the same subform then this code should work in the click event of the button
    (This is JavaScript code so make sure the code is set to JavaScript and Client on the drop downs in the script window)
    (assuming the name of the textfield is TextField1)
    TextField1.rawValue = "some value";
    If they are in different subforms then you have 2 options
    Please note in both these options it is easier if the subforms have names ( I am assuming this to keep samples simple)
    option 1 -
    use the parent object to move up the tree till you are at the same level as that of the subform that contains the textfield
    e.g. 
    (Click Event of the button)
    this.parent.subformname.TextField1.rawValue = "some value";
    option 2 -
    Use the resolve node to make your way down from the top level of the form
    xfa.resolveNode ("form1.subformname.TextField1").rawValue = "some value";
    Hope this helps
    Malcolm
    p.s. I am making assumptions as the image/file you attached did not appear for me.

  • Inserting Text into a web page

    I need to know how to insert text into a webpage, like google, search, and then get the result.
    Using the wonders of the internet I have been able to display a webpage, and see the HTML code that is generated, however, I don't know how to insert text into a web page, or get the result.
    Any help, or pointing into the right direction would be appreciated.

    What do you mean by insert into web page? Like
    prefill data on a form?
    Or like a search on google? What are you using to
    do this?
    I am guessing based on the info you gave in your
    question, that you might want to just look at the url
    of the page. For example search on google and you
    will see the text entered into the search field
    inside the url. Learn from that and develop a url
    that will search for the text you want to insert.
    If I am not understanding you, please clarify.Right now, I'm using the URL to search for me, which will work, but the search can be different from website to website, and I would like something closer to an automated form filler if I need to implement something like this again.
    Of course, something like an automated form filler would probably work better, if I were able to retreve the results

  • Inserting text into table cells in Powerpoint

    Hi
    I have a named table ClientInfo on Slide 3 in Powerpoint. How can I programmatically insert text into the table's cells?
    Thanks
    Regards

    Hi Yahya,
    What did you mean named table? Did you mean the name of shape?
    If I understood correctly, we can use table object to manipulate a table shape on a slide. And here is a sample to insert text into the first cell for your reference:
    Application.ActivePresentation.Slides(3).Shapes("ClientInfo").Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "Hello"
    Also here are some helpful links for learning PowerPoint developing:
    How do I... (PowerPoint 2013 developer reference)
    Object model reference (PowerPoint 2013 developer reference)
    Regards & Fei
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Cant insert Text into a TextBox. (Confusing Problem, CS6)

    Hello forum members!
    Just got the trial today and I am trying to insert text into a document. I press "T" and drag a box for a textbox. I then double-click inside that box to type something, and whatever I type absolutely NOTHING appears. It just blinks and nothing is actually typed inside the box.
    This is really frustrating. I have only just started and have one document that is fresh, so my question is what is the problem here?
    Thanks in advance,
    Shane Stocks

    Could you elaborate on that a little? I have taken a screenshot of my screen to show you how everything is. I have not touched any settings and have literally downloaded the trial about an hour ago.
    I do this. Click on the T, drag a box, then start typing. When I type, the icon does not move, and for some reason it seems what I am typing renames the layer tab. Please view the image, everything is there.
    Thanks again,
    P.S. The text-box does dissapear when the icon changes into a text icon, and the image above is me typing random things. Nothing appears.

  • HT2488 How to I insert text into a this command in automator wine "Text goes here" this is a perl script

    I have spent hours trying to figure this out, automator is the hardest thing to figure out. All I want to do is emulate me doing this
    -Open terminal
    -type wine
    -drag and drop the location of an .exe file
    -hit enter
    Now this should be easy as all I have to do is do run shell script, insert argument and done, BUT IT DOESNT RECOGNIZE WINE!?!?! Pleas help me.
    Let me clarify.
    If I make an automator application that that runs the command "wine" and then inserts text into the space after that Then i will be set. But no matter what I do nothing works. If i set the shell to perl and pass input as argument....It doesnt work the $f and $@ stuff doesnt work.
    Wine is likely not recognized because is is pearl based so I set it to pearl, but how do i pass and argument over to it?

    Try
    /opt/local/bin/wine "$@"
    The "..." will protect the white space, but still allow $variable subsitutions.
    The $@ will substitute all the files you drag as a group to your Automator app.
    The notation "$@" does the substitution with each file properly quoted to protect the white space in the file path, but provide each file as its own quoted argument to the 'wine' command line.
    If you need to invoke 'wine' once for each file of a group of files you drag on to your automator app, then use
    for f in "$@"
    do
         /opt/local/bin/wine "$f"
    done
    This will loop through each file of a group of files you drag onto your automator app, and invoke 'wine' once for each file in the group.

  • Freeform to Text: How to insert Text into freeform

    How to edit text into freeform

    Hi FZRR,
    Could you share more information with us? Or you can upload the screenshot to OneDrive and share the picture link here.
    If you want to insert text into freeform shape, there are two methods:
    1. Insert a textbox on the freeform shape, edit text in the textbox.
    2. Select the freeform shape, and right-click > Add text.
    If there is anything I can do for you regarding this issue, feel free to post back.
    Best regards,
    Greta Ge
    TechNet Community Support
    It's recommended to download and install
    Configuration Analyzer Tool (OffCAT), which is developed by Microsoft Support teams. Once the tool is installed, you can run it at any time to scan for hundreds of known issues in Office
    programs.

  • Inserting text into a Pages document with Applescript

    I want to create a Pages document from a custom template but be able to insert text at various points in the document (inputted data under headings etc). I would like to use Applescript but cannot get insertion points to work, is this possible? As usual, all help gratefully accepted.

    I have made a Pages template that contains tables and I want to enter text into sections of these tables
    That's a completely different story! But that's still possible using GUI Scripting. Here's an example:
    tell application "Pages"
        activate
        select table 1 of body text of front document
        tell application "System Events" to tell process "Pages"
            set theWindow to window 1 whose role description is "standard window"
            click button "Table" of tool bar 1 of theWindow
            keystroke "Cell A1"
            keystroke tab
            keystroke "Cell B1"
            key code 125 -- down arrow
            keystroke "Cell B2"
            -- and so on
        end tell
        select insertion point after table 1 of body text of front document
    end tell

  • Can't insert text into web page plus script error message

    I'm using CS3 and, on a fairly regular basis, when I am either pasting or inserting content (usually a newsletter of 500-1000 words), I get a message saying something like I can't insert text in this area. I definitely can insert text in the area as I have done it tons of times before. I sometimes have to cut and past a doc line by line to get it to insert. Very frustrating!
    Also, just recently, I've been getting a script error message, "An error has occurred in the script of this page" followed by Line: 992, Char: 106, Error: Unterminated String Constant, followed by the question: Do you want to continue running scripts on this page? I click Yes and everything seems to work fine.
    Help on either would be appreciated.
    Thanks in advance.

    You might want to make sure you are trying to copy plain text. It's possible that if you are copying from a Word document or another Web site that you are bringing along extra unseen code it doesn't like.  Try copying into Notepad first and then copying from Notepad into Contribute.  Not sure if that's the problem or not, but that's what came to my head first.
    I used to get an error that sounds similar (however it's not reproducing the error for me right now) to your error.  It was because some of my users had javascript menus on their page that Contribute didn't like.  However, if worked fine when you clicked yes and it was fine once it was published.   So if you are running a script that's probably why and I wouldn't worry too much about it as long as you can continue working and the page displays to your users correctly once published.  It's just annoying.
    Hope that helps.  Good luck.

  • Is there a way of inserting symbols into JTextPane?

    Hi there folks. Is there a way of inserting symbols like greek letters in a standard way into JTextPane?

    use getDocument(). insertString(offs,str, attributes);
    str should contain proper unicode chars and attributes must contain proper font family to show the chars.
    use StyleConstants.setFontFamily()
    regards,
    Stas

  • Inserting text into existing text in Powerpoint

    Hi
    I have a text like "I have included xxx in the quote." in a slide. How can I insert text such as '8 items per head' in place of xxx via code?
    Thanks
    Regards

    Hi,
    >> How can I insert text such as '8 items per head' in place of xxx via code .
    You can use Replace () method to replace "xxx" .Here is the sample code for you , which you can refer to replace "XXX" in the textRange in your PowerPoint  
    Set myDocument = ActivePresentation.Slides(2)
    Dim str
    For Each s In myDocument.Shapes
    If s.HasTextFrame And s.TextFrame.HasText Then
      str = s.TextFrame.TextRange.Text
      s.TextFrame.TextRange.Text = Replace(str, "xxx", "8 items per head")
       End If
    Next
    Best Regards
    Lan
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Inserting text into a PDF

    Greetings all!
    I'm quite new to InDesign, but I'm thrilled to see such a vibrant community on the official forums. I'm getting along quite well with it, absolutley awesome program, I may be in love...
    But, I've hit something of a problem. I'm making a digital sales brouchre, 99% of the text it it will remain the same, but there are a few sections where text will need to be changed by the sales guys in our office (client name, client requirements and costings)
    Acrobat Pro is too expensive for such simple edits, and I've tried inserting form fields as a workaround but found out that you cannot change the font from Times!
    I'm using InDesign CS6. Is there anything I can do to make these small sections easily editable? This document will be edited by end users so needless to say it needs to be simple!
    Failing that is there any way the text can be inserted dynamically? For example, the sales guys insert the text and costs into a web form, and the PDF is spat out at the other end?
    I know PDF's are finalised files, but I hope there is an adaquate workaround! Anything to get them to stop using Microsoft Word!!!

    Hi Mike,
    Thanks for taking the time to respond.
    The brochure is for digital use only, so I would need to find a way to edit it directly. It's only a simple edit, but I think I've found something.
    In the meantime, I'm using the (newly free!) version of Acrobat Pro 7 and using the TouchUp Text tool. Luckily the whole document displays correctly, and edits can be made. Although there are some limitations.
    For anyone having this issue you can download Acrobat Pro 7, along with the rest of CS2 totally free here: http://www.adobe.com/downloads/cs2_downloads/index.html
    This solution seems quite strong, but if anyone has a better solution, I'd love to hear it!

  • 'Inserting' text into a Text Field on event trigger in a static form

    Sorry if this seems like a silly question, but I've been struggling with it for a bit and I'm new to Adobe/JavaScript.
    I am building a static form (it must be static), and I want to have text appear/disapear based on an event trigger (mouseclick). I tried creating a floating text object inside of a static text object, but I'm not sure how I can make the string value for this change based on the event trigger. The only advice in the Adobe help was purely regarding FormCalc, which I can't use as this must be a static form. I tried using this code in the <script>, but it obviously didn't work as it was written from FormCalc code versus JaveScript;
    TextField.rawvalue = variable.value;
    What do you suggest in terms of being able to script this, so my static form will work? Thanks a billion if you can help!
    PS: I'm using Adobe 9, LiveCycle ES 8.2
    PPS: Also tried inserting the following into my trigger script;
                        xfa.resolveNode("form1.subform.TestField").rawValue= "Please Work";
                        xfa.layout.relayout("form1.subform.TestField");
    It also did not work.

    Hi,
    If the button and the textfield are on in the same subform then this code should work in the click event of the button
    (This is JavaScript code so make sure the code is set to JavaScript and Client on the drop downs in the script window)
    (assuming the name of the textfield is TextField1)
    TextField1.rawValue = "some value";
    If they are in different subforms then you have 2 options
    Please note in both these options it is easier if the subforms have names ( I am assuming this to keep samples simple)
    option 1 -
    use the parent object to move up the tree till you are at the same level as that of the subform that contains the textfield
    e.g. 
    (Click Event of the button)
    this.parent.subformname.TextField1.rawValue = "some value";
    option 2 -
    Use the resolve node to make your way down from the top level of the form
    xfa.resolveNode ("form1.subformname.TextField1").rawValue = "some value";
    Hope this helps
    Malcolm
    p.s. I am making assumptions as the image/file you attached did not appear for me.

Maybe you are looking for