Word wrap with JTextPane

Ok, at the risk of sounding stupid: How do you turn off the word wrap on a JTextPane.
Thanks in advance, Ed

...and I forgot setSize public class NonScrollingPane extends JTextPane { 
  public NonScrollingPane() {   
    super(); 
  public boolean getScrollableTracksViewportWidth() {
    return false; 
  public void setSize(Dimension d) {
    if(d.width < getParent().getSize().width) {
      d.width = getParent().getSize().width;
    super.setSize(d);
}Ulrich

Similar Messages

  • Word wrapping with JTextPane and styleDocument

    Hello,
    Can someone help me with the following problem.
    I have a JTextPane where i add a styledDocument.
    If i fill this with text it doen't word-warp the text!
    import javax.swing.JTextPane;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Style;
    import javax.swing.text.StyledDocument;
    public class LogField extends JTextPane implements LogFileTailerListener{
         public static StyledDocument doc;
         private LogFileTailer tailer;
         private TextStyles styles;
         public LogField(){
              this.setEditable(false);
              this.setContentType("text/html");
              doc = (StyledDocument)this.getDocument();
              styles = new TextStyles();
              tailer = new LogFileTailer( LogPanel.logFileName, 1000, true );
              tailer.addLogFileTailerListener( this );
             tailer.start();          
         public void addText(String text, Style style){     
               try {
                  text = text + System.getProperty("line.separator");
                   doc.insertString(doc.getLength(), text, style);
              } catch (BadLocationException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              this.setCaretPosition(doc.getLength());
         @Override
         public void newLogFileLine(String line) {
                addText(line,styles.getDefaultStyle());
    }

    Since you are simply appending text to the Document you should not be using a type of "text/html".
    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.

  • Force word wrap in JTextPane (using HTMLEditorKit)

    Hi!
    I have a JTextPane on a JScrollPane inside a JPanel. My text pane is using HTMLEditorKit.
    I set the scrolling policies of my JScrollPane to: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED & JScrollPane.HORIZONTAL_SCROLLBAR_NEVER.
    I did not find in JTextPane�s API any relevant property I can control word-wrapping with (similar to JTextArea�s setLineWrap) and I have tried a few things to force word-wrapping but nothing worked...
    What happens is my text is NOT being wrapped (even though there is no horizontal scrolling) and typed characters (it is an enabled editable text componenet) are being added to the same long line�
    I�d like to force word-wrapping (always) using the vertical scroll bar whenever the texts extends for more then one (wrapped) line.
    Any ideas?
    Thanks!!
    Message was edited by:
    io1

    Your suggestion of using the example only holds if it fits the requirements of the featureDid your questions state anywhere what your requirement was?
    You first questions said you had a problem, so I gave you a solution
    You next posting stated what your where currently doing, but it didn't say it was a requirement to do it that way. So again I suggested you change your code to match the working example.
    Finally on your third posting you state you have some server related issues causing the requirement which I did not know about in my first two postings.
    State your problem and special requirments up front so we don't waste time quessing.
    I've used code like this and it wraps fine:
    JTextPane textPane = new JTextPane();
    textPane.setContentType( "text/html" );
    HTMLEditorKit editorKit = (HTMLEditorKit)textPane.getEditorKit();
    HTMLDocument doc = (HTMLDocument)textPane.getDocument();
    textPane.setText( "<html><body>some long text ...</body></html>" );
    JScrollPane scrollPane = new JScrollPane( textPane );If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.

  • Word Wrap in JTextPane

    I'm working with just a JTextPane, no JScrollPane around it or anything.
    I would like the JTextPane to use word wrap, but it looks like by default it does not. I looked through some API stuff and tutorials and found nothing on the subject.
    The code is trivial and pointless to post right now. All I dd was make a JTextPane via new JTextPane() (no arguments or anything) and type in it enough to see if it word wrapped and it didn't.
    Is there a way to enable word wrap on JTextPanes?

    no JScrollPane around it or anything.The scroll pane forces the wrapping because it enforces the width limit. Otherwise there is no limit.
    You can use setPreferredSize(...) to force the width, but then that also restricts the height.
    Reply 6 of this posting may give you some ideas.

  • Word Wrap with a Vertical Grid

    I've got a vertical grid with my headers in the left column and descriptive text in the column immediately to the right.  I'd like to have the descriptive text word wrap but checking the word wrap box on the layout tab doesn't seem to have any effect.
    Any ideas?  I'm using 11.5 SP3

    I'm not sure if it will work with the iGrid in a vertical orientation, but have you tried setting the RowHeight to something like 20 or 30 instead of the default of 0?  This setting will auto adjust the grid lines based upon the font-size, but you may need to force it to a certain height in order for the wrapping to work.
    Regards,
    Jeremy

  • Preventing word wrapping in JTextpane

    I'm stumped about preventing word wrapping in a JTextpane. Any suggestions?

    Word wrapping occurs when the text hits the "edge" of the JTextPane, as (ordinarily) defined by the parent component of the JTextPane. But if you view the JTextPane in a JScrollPane, the edge is defined by the JTextPane itself rather than the parent component. So if the JTextPane is wide enough, the text will not wrap (you may or may not need to call setSize on the JTextPane). You can turn off the horizontal scrollbar if you like, using JScrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER), which would cause any text wider than the JScrollPane parent to simply be clipped off.
    A code sample is usually available at http://www.radecke.ch/exscrolltextpane.htm (the site appears to be down at the moment).

  • Word wrap with jLabel

    Is it possible to word-wrap text in a jLabel field? If not, what would be a good choice of component?

    displayArea.setLineWrap (true);
    displayArea.setWrapStyleWord (true);I thought it wasn't added automatic.
    Kind regards,
      Levi

  • Word wrap with JScrollPane

    Quick question. Trying to get word wrap in a JTextField
    using JSrollPane. Below is a small snippet of code
    from my program...
    displayArea = new JTextArea ( );
    JScrollPane scroll = new JScrollPane ( displayArea,
       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
       JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); Doesn't using JScrollPane.HORIZONTAL_SCROLLBAR_NEVER automatically
    introduce word wrap? I'm not getting it in my program. Somebody please correct me. Thanks!

    displayArea.setLineWrap (true);
    displayArea.setWrapStyleWord (true);I thought it wasn't added automatic.
    Kind regards,
      Levi

  • Word wrapping at JTextPane boundaries

    Hi ! I have two problems related to JTextPane -
    1. I want to wrap words which are larger than the width of JTextPane so that it may remain visible inside the fixed boundaries.
    2. if there is an image and it can not be displayed in the same line in continuation of text then it should be displayed in the next line.
    All images that i have used have less width than that of JTextPane.
    Both these problems may be related or different, I dont know how to solve these problems.
    Please suggest any solution.

    Till now I am able to find the height of JTextPane after inserting some text by creating a dummy JTextPane
    and calling the method getPrefferedSize().height
    this wraps text correctly at word boundaries but i want it to be wrapped according to word boundaries and
    textpane boundaries both
    actually I am developing a chat application in which I have to show message from other person
    and if there is some text like ": )", ": D" etc then i want to show smileys :), :D in the place of these substrings
    I can insert text and replace the text with images by using StyledDocument's insert function
    but if i simply replace the text with images then the images are shown in the same line and wrapping doesn't work
    hence some part of images are not visible in the JTextPane
    as shown in the links http://www.facebook.com/photo.php?fbid=193222607456431&set=a.193222600789765.36992.100003060781951&type=1&ref=nf
    and http://www.facebook.com/photo.php?fbid=193225164122842&set=a.193222600789765.36992.100003060781951&type=1&ref=nf
    here link 1 shows the problem of partial display of image
    and link 2 shows the problem of inserting text that is larger than the width of the JTextPane
    And i want that whatever is inside the JTextPane should be shown inside the boundaries of it, I don't want to add a
    horizontal scrollbar !!!!!
    hope you got the problem, please provide some help !

  • Word wrap with Flex 4

    Hi, Can someone help me in getting a wordwrap in Spark. Below is my code. Any help regarding this is highly Appreciated
    I  am trying to get wordwrap within a spark component, but I couldn't. Can someone fix this. Usoing Label or richtext or any control but not textinput or richeditabletext.
    I am also trying to copy paste the text. But I could not do that. How to do that?
    <?xml version="1.0" encoding="utf-8"?>
    <s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="
    library://ns.adobe.com/flex/spark" xmlns:mx="
    library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:com="com.*" xmlns:local="*">
    <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Panel x="229" y="158" width="250" height="200" >
    <s:VGroup id="vGroup1" top="5" bottom="5" left="5" right="5" gap="-2" variableRowHeight="true" width="240" height="190">
    <s:HGroup width="100%" gap="0" id="hGroup1" left="5" height="100%">
    <s:VGroup width="100%" id="vGroup2" gap="-2" right="35" height="100%">  
    <mx:Text id="t" fontWeight="bold" htmlText="You can format the text in a Text control using HTML tags, which are applied after the control's CSS 
    />  
    <mx:Text id="DIS_Subject" width="100%" htmlText="The Text hhhhhhhhh control displays multiline"/>
    </s:VGroup>
    <mx:Image id="iconComplete" width="16"/>
    </s:HGroup>  
    <mx:Text textIndent="20" text="using HTML tags, which are hhhhhhhhh 888899999999 end " id="DIS_Author" width="100%" height="100%"/>
    <mx:Text text="ext control displays m mmmmmmmmmmmmm 5555555555end" id="lastReply" width="90%" height="100%"/>  
    </s:VGroup>
    </s:Panel></s:Application>

    well, it's more of a behavior, for lack of a better word, of  the component's width (rather than a documented property) You won't find it in the class definition. So, you just need to set the width.  In your code, you originally had;
    <mx:Text id="t" fontWeight="bold" htmlText="You can format the text in a Text control using HTML tags,which are applied after the control's CSS"/>
    you just need to set the width for that component, something like  <mx:Text id="t" width="100%" . . . />  just as you did with the others.  As soon as I set a width on it, it wrapped.
    * note, after setting the width, be sure to 'refresh' your design view to update your changes.

  • Spark RichText - no word wrap with percentage width

    Hello,
    With the old mx Text component, I could specify a percentage width, and have the height of the Text component automatically increase to show all the text.  I'm switching over to use the spark RichText component, and now this automatic wordwrap does not work with a percentage width (or by setting both left and right styles) - it just displays the first line of text and that's it.  It still works if I set an explicit width in pixels, but I want to be able to set a percentage width.
    Is there a way to get the height to calculate automatically like it did before when using a percentage width?

    Thanks for looking at it.  After some more digging, I found that it was because the RichText was inside a mx Canvas container.  I replaced it with a spark Group and it worked.

  • Word wrap in JList

    I was wondering if I can do a word wrap with JLists.
    I have this so far.
    JList pizza;
    DefaultListModel listPizza;
    JScrollPane scrollPizza;
    listPizza = new DefaultListModel();
    pizza = new JList(listPizza);
    scrollPizza = new JScrollPane(pizza);
    This will let me scroll horizontally if the text becomes longer than the screen, but I want it to wrap around the screen. I only want to it to scroll vertically.

    In the future, swing questions should be posted in the swing forum.
    If I recall correctly, you can do this by making a custom CellRenderer for your JList which uses a JTextArea to do the rendering. Search the swing forum or look at the sun java tutorial for custom renderers.

  • Word wrapping incorrect inside JTextPane on MAC 0.5/10.4 and Linux OS

    Hello java-dev team,
    In my application, I am using JTextPane as a text editor to create RTF text. The problem I observed with the JTextPane on MAC OS and Linux OS flavors (I tested on Ubuntu 8.04) is: whenever I am changing any of the text property (font color, font name, font style like bold, italic or underline) and then enter the characters without providing space, the whole word gets wrapped to the next line from the point where exactly the property change starts i.e. the new formatted text is jumped to the next line from the starting point of formatting.
    My requirement is, as I am not adding any space character while changing the property, the characters should be entered in the sequence and should come to new line as per normal word-wrap rule.
    Can anybody help me out in this regards with some solution as it’s a very urgent requirement?
    Below I am also providing the sample code to check the behavior. There is no dynamic facility to change the property in the below code but I am providing the text with multiple formatting settings through code itself. To reproduce the above issue, just type the characters using keyboard in between characters with changed text properties and you can see the whole word jumping to new line.
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;
    import javax.swing.text.StyledEditorKit;
    public class TestWrapping {
         JScrollPane scroll;
         JTextPane edit;
         public TestWrapping()  throws Exception {
              final JFrame frame=new JFrame();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              edit=new JTextPane()
                   public void setSize(Dimension d)
                        if (d.width < getParent().getSize().width)
                             d.width = getParent().getSize().width;
                        super.setSize(d);
                   public boolean getScrollableTracksViewportWidth()
                        return false;
              edit.setPreferredSize(new Dimension(150,150));
              edit.setSize(150,150);
              edit.setEditorKit(new StyledEditorKit());
              scroll=new JScrollPane(edit);
              edit.getDocument().insertString(0,"11111111111111222222222222222222222222222222222222222",null);
              MutableAttributeSet attrs=new SimpleAttributeSet();
              StyleConstants.setBold(attrs,true);
              ((StyledDocument)edit.getDocument()).setCharacterAttributes(30,5,attrs,false);
              StyleConstants.setUnderline(attrs,true);
              ((StyledDocument)edit.getDocument()).setCharacterAttributes(35,5,attrs,false);
              StyleConstants.setFontSize(attrs,25);
              ((StyledDocument)edit.getDocument()).setCharacterAttributes(40,5,attrs,false);
              frame.getContentPane().add(scroll);
              frame.setBounds(0,0,150,150);
              frame.show();
         public static void main(String[] args) throws Exception {
              new TestWrapping();
    }The same functionality works fine on Windows XP and problem is observed on MAC OS 10.4/10.5, Ubuntu 8.04. I am using jdk1.5 on both the platforms. I tested using jdk 6.0 on Ubuntu 8.04, and the problem is easily reproducible. I placed a query in the respective OS forums also but did not receive any replies. I also searched the sun’s bug database, but could not find any bug addressing the mentioned problem correctly. I also logged issue at bugs.sun.com and was assigned internal review ID of 1480019 for this problem.
    If you need any additional information, please let me know.
    Thanks in advance.
    Regards,
    VPKVL

    VPKVL wrote:
    Hi All,
    Just want to update that when I checked this issue with JDK1.6 on Ubuntu 8.04, I am unable to reproduce the issue. Everything works as expected. But problem continues on MAC 10.5. So can anybody help me out in coming out of this issue on MAC 10.5.
    Thanks in advance.The only thing I can suggest is that you open a bug (radar) with apple ( developer.apple.com ), and then wait at least 12 months for Apple to get around to fixing it :s

  • Read Only TextAreas with Carriage Return, Line Breaks and Word Wrapping

    Hi all,
    I know there are a few posts around this subject but I cannot find the answer to the exact problem I have.
    I have a page that has a 'TextArea with Character Counter' (4000 Chars) that is conditionally read only based on the users credentials (using the 'Read Only' attributes of the TextArea item).
    When the field is editable (not Read Only) everything works fine but when I make the field Read Only I start to have problems:
    The first problem is that the Carriage Return and Line Breaks are ignored and the text becomes one continuos block. I have managed to fix this by adding pre and post element text of pre and /pre tags. This has made the Carriage Return and Line Breaks word nicely and dispaly correctly.
    However, it has introduced a second problem. Long lines, with no Carriage Returns or Line Breaks, now extend to the far right of the page with no word wrapping, making my page potentially 4000+ characters wide.
    How can I get the field to be display only, with recognised Carriage Returns and Line Breaks, and Word Wrapping inside a fixed width of, say, 150 characters?
    Many thanks,
    Martin

    Hi,
    Just a cut and paste of yours with the field name changed:
    htp.p('<script>');
    htp.p('$x("P3_COMMENTS").readonly=true;');
    htp.p('</script>');I also have the following in the page HTML Header, could they be conflicting?
    <script type="text/javascript" language="JavaScript">
    function setReleaseToProd(wpTypeCode){
       //setReleaseToProd($v(this))
      var get = new htmldb_Get(null,$v('pFlowId'),'APPLICATION_PROCESS=set_release_to_prod',0);
      get.addParam('x01',wpTypeCode);
      gReturn = get.get();
      if(gReturn) {
         $s('P3_RELEASE_TO_PROD',gReturn);
      get = null;
    </script>I am a long way from knowing much about Javascript (this page code was written by someone else) so all help is much appreciated.
    Martin

  • JTextPane always word wrapping

    I'm having problems convincing a JTextPane not to word wrap. My JTextPane is contained in a JScrollPane. I've looked and found out how to turn off word wrap in a JTextArea, and applied suggestions about turning horizontal scrollbars on, but it doesn't work. My JTextPane keeps wrapping text regardless of what I do to it. It seems to me that, since the JTextPane is more "sophisticated" than a JTextArea, I should be able to do this. Any suggestions?
    Matt

    Hi,
    overwrite the following method in your JTextPane-subclass
    public boolean getScrollableTracksViewportWidth() { return false;  }this works, if you use the scrollbar-policies below - I use that in a mud-client, returning false if I want it not to wrap and returning true, if I want it to wrap.
    I use the following on the JScrollPane, the JTextPane is in (assuming sp is your JScrollPane)
    sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);Hope, this helps
    greetings Marsian

Maybe you are looking for

  • Ipad mini stuck on child mode

    ipad mini stuck on child mode.  won't scroll. 

  • Need to repair Rosetta

    In an attempt to free up some hard drive space i was searching the net and came upon this site for a program called Monolingual http://monolingual.sourceforge.net/ The program claimed to remove unnecessary languages from my mac and in turn free up sp

  • Problem with ADF Table Selection

    Experts, Working in jdev 11.1.1.3.0 I have search page, after search will select one row and based on that row rest all pages will navigate... everything is working fine. But once coming back to search page selection is there but when i trying to nav

  • BSOD may occurs when hibernating by Intel WiFi driver on Windows 7 x64

    I use Thikpad X201s (5129CTO). BSOD has occured today. The content of the event log is shown in the following. - System - Provider [ Name] Microsoft-Windows-Kernel-Power [ Guid] {331C3B3A-2005-44C2-AC5E-77220C37D6B4} EventID 41 Version 2 Level 1 Task

  • About this Critial Error Message exception: com.ms.security.SecurityExcepti

    Can anyone briefly help me how to solve this problem in a Chat Client exception: com.ms.security.SecurityExceptionEx[Client.establishSockConnection]:cannot access "202.53.22.35"