HTML TextArea is a  JScrollPane in JEditorPane?

Hi all,
I'm trying to create an autofill facility for my application, to do this I am getting the components from the HTML document and converting them to their Java Swing counterpart.
So for example <input type="text" name="example" /> would be a normal text input field on a HTML form, and its Swing conversion is a JTextField - this works as desired and it allows me to use the .settext() command to set the text on the HTML form - i.e. autofilling.
My problem comes when I try to get the component for a HTML textarea, e.g. <textarea name="example"></textarea> - I would expect the Swing component to be a JTextArea, but the class it gets converted to is a JSrcollPane! This leads to a problem as I can't set the text of a JScrollPane as it is a container.
Does anyone know how I might get around this problem so that I can set the text for the textarea? I've included my code below.
Many thanks
BBB
import java.awt.event.*;
import java.awt.*;
import java.net.URL;
import javax.swing.*;
public class Tester extends JFrame{
    static JEditorPane pane = new JEditorPane();
    public Tester()
        try {
            pane.setPage( new URL("http://www.amray.com/cgi/amray/addurl.cgi") );
        } catch (Exception e) {
        this.getContentPane().add(new JScrollPane(pane));
        JButton b1 = new JButton("Auto Fill");
        this.getContentPane().add(b1,BorderLayout.SOUTH);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
                for ( int i = 0; i < pane.getComponentCount(); i++ )
                    Container c = (Container)pane.getComponent(i);
                    Component swingComponentOfHTMLInputType = c.getComponent(0);
                    System.out.println(swingComponentOfHTMLInputType.getClass());
                    if ( swingComponentOfHTMLInputType instanceof JTextField ) {
                        JTextField tf = (JTextField)swingComponentOfHTMLInputType;
                        tf.setBackground( Color.yellow );
                        tf.setText("Auto Filled");
                    if ( swingComponentOfHTMLInputType instanceof JScrollPane ) {
                        JScrollPane ta = (JScrollPane)swingComponentOfHTMLInputType;
    public static void main(String args[]) {
        Tester app = new Tester();
        app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        app.setSize( 400, 400 );
        app.setVisible( true );
}

Hi Jim,
Thanks for the suggestion, but I've already tried that and although it does work from a visual POV, when you then submit the form the data in the JTextArea isn't added - it just says the field is blank.
Although, I'm not sure whether it's not working due to the method I've had to use to add the JTextArea: -
JScrollPane ta = (JScrollPane)swingComponentOfHTMLInputType;
JTextArea oTextArea = new JTextArea();
oTextArea.setText("Hello World!");
ta.setViewportView(oTextArea);I'm not sure if it's not working because I've had to use .setViewportView(); rather than .add(). If I use .add() then no changes are reflected on screen and the JTextArea isn't actually added.
Any other suggestions?

Similar Messages

  • JEditorPane + html TEXTAREA + WRAP

    Hi, I have a simple program that uses a JEditorPane to create an html TEXTAREA.
    Supposedly the text in the TEXTAREA should be wrapped, and when I add READONLY, the text can�t be deleted. However, the wrapping and readonly don�t work�
    Here is the code:
    import javax.swing.*;
    import java.awt.*;
    public class Main extends JFrame {
        public Main() {
            String abc = "A BIG LONG STRING WHICH SHOULD BE WRAPPED, AND CANNOT BE DELETE...A BIG LONG STRING WHICH SHOULD BE WRAPPED, AND CANNOT BE DELETE...A BIG LONG STRING WHICH SHOULD BE WRAPPED, AND CANNOT BE DELETE...A BIG LONG STRING WHICH SHOULD BE WRAPPED, AND CANNOT BE DELETE...";
            Container ct = getContentPane();
            JEditorPane ep = new JEditorPane();
            ep.setContentType("text/html");
            ep.setText("<body><tr><td class=one width=135>Test Criteria:</b></td><TEXTAREA WRAP=SOFT COLS=20 ROWS=2 READONLY>"
                       + abc + "</TEXTAREA></tr></body>");
            ct.add("Center", ep);
            setSize(400, 300);
            setVisible(true);
        public static void main(String[] args) {
            new Main();
    }Please help me out.
    Thanks.
    Hong Jiang

    As I understand it, JEditorPane's version of HTML is 3.2. Looking in the HTML 3.2 recommendation I don't see "wrap" and "readonly" as acceptable attributes for Textarea.

  • To display value in html: textarea in struts

    Hello,
    I want to display value in <html:textarea> in struts application from my userdefine FormBean.
    i tried like this
    <html:textarea property="documentNumberList" value='"<bean:write name="QualityForm" property="displayDocumentNumber">" ></html:textarea>
    I am not getting success.
    I would be appriciate to you if anyone could let me know what is the solution of this problem ? or send me one example.
    Thanks
    Rajnish
    [email protected]

    Hi,
    That did'nt solve my problem. Actually i am trying to get the arraylist data one by one into different textareas. There are two arrays one is quesArray and the other quesIdArray. I need the quesIdArray in the individual text areas.How can i achieve this?
    I am using the following code to do that.
    <logic:iterate id="item" name="selectionCriteriaForm" indexId="index" property="quesArray">
    <tr><td><li><em><b><bean:write name="item"/> </em></li></b></td>          
    <td><html:textarea property="quesIdArray" cols="50" rows="2" name="selectionCriteriaForm" onkeyup="limitTextarea(answer<%=i %>,5,50)" />          
    <br></td></tr>
    <% i=i+1;
    %></td></tr>          
    </logic:iterate>
    Pls help me on this.
    Thanx in advance.

  • Map html tags to text display in jeditorpane

    hi,
    i am looking for some way to map the position of a tag in a html i/p to the actual display in the JEditorPane... below are the details of why i need this feature...
    i have a JEditorPane whose text is set from a html file i/p... and i have a JTree which is like a table of contents for the content in the JEditorPane... when i click on any of the jTree nodes, i want to scroll the JEditorPane to the position corresponding to that element in the text...
    currently i am doing a string search (for ex, "1. My_First_Heading: ") and getting the position from the editorPane.getDocument().getText() method and then using a modelToView() to determine the scrollBar position...
    but i would like to have a more correct searching method, since the string could appear at many places... i would like to have some anchor or some tags defined at the place of each table of content element in the JEditorPane's html i/p... then search for those tags and somehow map the position in the html to the position in the display...
    is there a way i can do this...
    thanks,
    prasuna

    Hi Tarabyte,
    Actually wanted to know how to map the postion of any tags in the html to the display in the jeditorpane...
    Hi StanislavL,
    Thank you very much for your answer... it solved my problem... awarded the 5 duke dollars to you for that...
    Cheers,
    Prasuna

  • Default text for html:textarea..

    Hi All.
    How to populate a textarea in struts(<html:textarea>) with some default text.
    Waiting for your suggestion in this regard.

    Why are you duplicating the answer after 2 days? The answer is already given. Just leave the thread as it is as long there are no unanswered questions from the topicstarter.

  • Dynamically scrollable html:textarea

    Hi,
    Can someone please tell me how to make a html:text area scrollable and dynamic(rows are populated from DB). I am using struts and am populating in the jsp page using logic:iterate tags.

    An HTML textarea becomes scrollable as soon as it's content spans rows greater than the display. This is automatically done by the browser. You need to specify how may rows the textarea should display ( this decides how large it will be, vertically ) and that's it.

  • Struts "bean:write" in "html:textarea"

    Hi,
    i want to write a bean with bean:write to a html:textarea
    how can i do this?

    Hint:
    <html:textarea ...>
        <bean:write ... />
    </html:textarea>

  • Remove Html textarea scrollbar from jeditorpane

    Hi,
    I am using jeditorpane for display a content in my project,
    i am create html code and set to jeditorpane, but in my code i am using textarea for display large text,
    what my problem is in the textarea is not at all wrap and even small text also its show scrollbar,
    i want to remove the scrollbar and wrap properly, can any one give some idea is highly useful to solve my problem.
    my code is
            setContentType("text/html"_);
              HTMLEditorKit kit = new HTMLEditorKit();
              super.setEditorKit(kit);
              StyleSheet styleSheet = kit.getStyleSheet();
              styleSheet.addRule("textarea {style=border:4; overflow:auto; wrap:virtual}");
              styleSheet.addRule("body {color:#000; font-family:times; margin: 1px }");
              StringBuffer sb = new StringBuffer();
            // in this string buffer i am append all the html code
             setText( sb.toString() );Thanks in advance.

    This is the technique I use.
    1. Create 2 identical columns, one with the HTML and one without.
    2. Then conditionally display the columns, one to display in the web page and the other to display in the download. This can easily be done using the request value e.g.
    Condition Type: Request Is NOT Contained within Expression 1
    Expression1: CSV,PDF,RTF,XLS
    I hope that does the trick for you :)
    Simom

  • Help regarding HTML Textarea in JSP

    I have a textarea in my JSP where if the text entered is </TEXTAREA> it considers it to be the code to end the Textarea.
    The code i have used is :
    <TEXTAREA cols="30" name="<%=fieldName + template.getFieldId()%>" ><%=fieldValue%></TEXTAREA>
    When the form is saved with the text ' </Textarea> abcd ' in the textarea then the page if retrived after saved is scrambled.

    You will want to escape those nasty < and > characters that are in your text.
    Turn them into &lt; and &gt; in your HTML and you should be ok.
    (plug time for JSTL and EL again)
    Using <c:out > tag in JSTL automatically escapes these nasty characters for you, unless you ask it not to.
    Struts provides a similar tag I believe.
    Good luck,
    evnafets

  • ERROR USING HTML textarea with Java String

    I have a form in HTML that has a textarea for input messages.
    When user press submit, a java servlet gets the information and stores it in a Database as a String.
    Later, I have a JSP thata retrieves that information from the database and put it as the value propertie of another textarea in the page so user can modify it if wanted, but I am having a hard time finding how to do so , because when I do the line:
    <textarea .....bla bla.... value=
    <%=some_string%>
    .....bla >
    and the some_string has more than one line ( e.g "\n" or "\r" caracteres ), I got an error when displaying the page. Can anyone help me with that please ???
    Thanks.

    here's my codes again for reference. hope it helps to rectify the matter
    // edit.jsp
    String search = (String)request.getParameter("txtSearch");
    String parameter = (String)"%"+search+"%";
    String sSQL = "select * from tblArticle where Title LIKE '"+parameter+"'";
    <%
    try {
    while(Rs.next()) {
    %>
    <tr>
    <td width="16%" height="157" align="center">Article:</td>
    <td width="88%" height="157"><textarea rows="15" name="txtArt" cols="86" value="<%= txtArt %>"></textarea></td>
    </tr>
    </table>
    <%
    // .....

  • Html tags br and hr in JEditorPane

    I have a JEditorPane that is used to display html data. The data is obtained from an xml file, transformed to html using a stylesheet and rendered in the JEditorPane using setContentType("text/html") and setText() methods. However the <br/> and <hr/> tags used in the html cause problems in that a '>' sign shows up at the location of these tags in the JEditorPane. So if I have a <hr/> in the html, a horizontal line followed by a > sign is rendered in the JEditorPane. I am unable to figure out whats going on, the html is rendered fine in browsers.
    Also is there a way to get rid of the <? xml version etc etc ?> tag at the beginning of the generated html without resorting to String manipulation exercises.
    Any help will be appreciated. I am using jdk 1.4
    Thanks in advance
    RS

    I am trying to figure out how to get rid of the pesky > signs. The html that I have used is pretty basic, just some text formatted by the <p>, <br> and <hr> tags., Is there any documentation on what JeditorPane supports (which html version or what tags) and what it does not.

  • HTML Textarea removing Carriage Returns

    Hi,
    Hoping someone can assist, I am retrieving some data from a table that currently consists of carriage returns but when I try and retrieve this information via Ajax and pass it back into the HTML page textarea field within ApEx, the value returned is one continuous string.
    All my carriage returns are removed.
    Can someone please assist, possibly using a javascript solution or some other type as to how I can preserve the carriage returns.
    Thanks.
    Tony.

    Hi Tony,
    I suspect the problem is simply due to the way HTML handles line breaks, which is to say, it generally ignores them. For example, this html:
    <p>To be or not to be, that is the question;
    Whether 'tis nobler in the mind to suffer
    The slings and arrows of outrageous fortune,
    Or to take arms against a sea of troubles,
    And by opposing, end them. To die, to sleep;
    No more; </p>would be displayed in a browser as:
    To be or not to be, that is the question; Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing, end them. To die, to sleep; No more;
    One way to get around this is to wrap the string in {font:Courier}&lt;pre&gt;&lt;/pre&gt;{font} tags; another would be to replace the carriage returns with {font:Courier}&lt;br&gt;{font}
    Hope this helps.
    tx, --Jen                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Suppressing certain HTML tags before setting text to JEditorPane

    Sir,
    I am setting the text(html format) for JEditorPane using the setText(String) method. But I need to suppress all the <IMG> tags that are present in this text before setting it to JEditorPane. Is there any way in which I can write my extended HTMLEditorKit wherein I can check for specific HTML.Tag and prevent it from getting added to the document.
    Can you please help me out with some example.
    Regards,
    Alex

    Instead of trying to extend the HTMLEditorKit, you don't you add a normal method to scan the text for the <IMG> tag and get rid of it yourself before putting it in the JEditorPane using the setText() method. You can try something like this:
    public String removeImgTag(String text) {
       String tmp=text.toLowerCase();
       int i=tmp.indexOf("<img");
       if (i<0) return text;
       int j=tmp.substring(i).indexOf(">");
       return tmp.substring(0,i-1)+tmp.substring(i+j);
    };o)
    V.V.

  • Html textArea loosing textFormat after link insert

    Hello,
    I'm trying to develope a texteditor for my websites in As3, but I'm facing a issue,
    I can make my selected text bold, italic, untherline etc, I can insert a link, to an email, per example, but if I try to insert more than one email link, my textarea looses all previously inserted bolds, italics, etc, even all the line breacks are gone.
    What can I do to avoid this,
    this is the piece of code to make an email link
    var inicio_seleccao:int;
    var fim_seleccao:int;
    inicio_seleccao = texto.selectionBeginIndex;
    fim_seleccao = texto.selectionEndIndex;
    var cpt = getChildByName("que_email");
    var em:String;
    em = "<A HREF=\"mailto:"+cpt.text+"\"><U>"+cpt.text+"</U></A>";
    var anterior:String = texto.text.slice(0, inicio_seleccao);
    var posterior:String = texto.text.slice(fim_seleccao, texto.length);
    texto.htmlText = anterior+" "+em+" "+posterior;
    this shold result in something like
    hello world, this is my email, please contact me
    where the word email is a link
    but if I try to do this
    hello world, this is my email, please contact me.
    and this is my website
    now, I just get something like
    hello world, this is my email, please contact me.and this is my website
    the line break desapears, the email link also and the websit has a link
    Thanks!

    try:
    var inicio_seleccao:int;
    var fim_seleccao:int;
    inicio_seleccao = texto.selectionBeginIndex;
    fim_seleccao = texto.selectionEndIndex;
    var cpt = getChildByName("que_email");
    var em:String;
    em = "<A HREF=\"mailto:"+cpt.text+"\"><U>"+cpt.text+"</U></A>";
    var anterior:String = texto.htmlText.slice(0, inicio_seleccao);
    var posterior:String = texto.htmlText.slice(fim_seleccao, texto.length);
    texto.htmlText = anterior+" "+em+" "+posterior;

  • How to insert HTML content directly in code from JEditorPane ?

    I'm developping an HTML Editor on a JEditor Pane.
    The problem is that a blank is not interpreted, and i've tried to add "& n b s p ;" in the code from a KeyListener added to the VK_SPACE event, or to define an Action to add HTML Spaces. But i didn't achieve to...
    I just want to insert a "& n b s p ;" in the HTML code of the document displayed in the JEditor evry time the SpaceBar is pressed.
    I've tried InsertContentAction() :
    Not a solution :It inserts " & n b s p ;" in the text (it appears litteraly).
    I've tried InsertHTML. But it needs a tag parameter, and in my case, i need no tag
    to be inserted.
    I'm stuck since days... Any help woul be extremely appreciated...

    The code I now have is:
    TextAction actInsertNBSP = new TextAction("insert-space"){    
    public void actionPerformed(ActionEvent evt){
    //Where is the cursor now
    int intInitialCaretPos = text.getCaretPosition();
    try{       
         //Make HTML document to add the html code
         HTMLDocument htmldoc = (HTMLDocument)text.getStyledDocument();
         //Get document input attributes before insert
         AttributeSet atr = kit.getInputAttributes().copyAttributes();
         //Insert tag
         kit.insertHTML(htmldoc,intInitialCaretPos,"<b>\240</b>", 0, 0, HTML.Tag.B);
         //Set stored attributes to input attributes
         kit.getInputAttributes().addAttributes(atr);
         //Set cursor on right position
         text.setCaretPosition(text.getCaretPosition());                    
         }catch (Exception exc){
              exc.printStackTrace();
    KeyStroke kstSpace = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
    text.getKeymap().addActionForKeyStroke(kstSpace,actInsertNBSP);

Maybe you are looking for