JTextArea failing proper word wrap within JList

A JTextArea is inserted into a JScrollPane which is being rendered in a JList. Each of these JTextAreas render correctly, except the text within them does not word wrap around the whitespace.
Example, instead of seeing
|Testing 1 2 3    |
|testing.         |
------------------we are seeing:
|Testing 1 2 3 tes|
|ting.            |
------------------I'm aware the JTextArea component may not have a chance to validate properly, since it isn't visible until it is drawn into the JList. (Note: lineWrap on the JTextArea is set to true).
Is there anything I can do to correct this? Or am I wrong about the cause and there's another solution?

I think you're looking for
JTextArea.setWrapStyleWord(true);
Normally I'd yell at you to read the API, but I
remember having a
hard time finding that one myself. :)Looking at the link camickr posted, I saw that in some example code and immediately checked it out. Tried it in my code, and it works.
I'm a big advocate on the API myself, shame on me for missing it! (Glad I'm not the only one tho)

Similar Messages

  • 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.

  • Thunderbird 31.6.0 sometimes fails to word wrap properly

    Text lines sometimes fail to word wrap properly. Example: a sentence that contains the words "foggy bottom" , where the "foggy" is at the end of one line (when composing) and the word "bottom" is wrapped to the beginning of the next line appears like this: "foggybottom" when it appears in the "sent" folder. The two words were separated by a single space when composing. Does not happen all the time, only occasionally. I cannot reproduce this problem on demand - it only happens at random.

    Can you confirm that you are using Plain Text, not HTML?
    do you use Plain Text all the time?
    Can you check the following and say what you have as settings:
    Tools > Options > Advanced > General tab
    or
    Menu icon > Options > Options > Advanced > General tab
    click on 'Config Editor' button
    it will tell you to be careful :)
    In top search type: flowed
    What Value do you have for the following:
    I have not changed anything, so this is the default settings on my system.
    * mailnews.display.disable_format_flowed_support; Value = false
    * mailnews.send_plaintext_flowed; Value = true
    In top search type: wrap
    All Value settings show what I have
    What Value do you have for the following?
    * mail.compose.wrap_to_window_width; Value = true
    * mail.wrap_long_lines; Value = true
    * mailnews.wraplength; Value = 72
    * plain_text.wrap_long_lines; Value = false
    * view_source.wrap_long_lines; Value = false
    In the Saved Draft or Sent folder.
    select the email so you can read contents in message pane
    click on 'Other Actions' and then 'View source'
    where it says 'content type does it also say 'format-flowed'
    eg:
    Content-Type: text/plain; charset=windows-1252; format=flowed
    Content-Transfer-Encoding: 7bit

  • How to word wrap within text field

    I've created a form with a text field but when filling the form out, the words don't word wrap, but merely go in a single line across the text box field. 
    When can I format the text box to word wrap?
    tks

    I have set my text fields to multi line and the text wraps but it only starts about halfway down the text box.  Please help, this is getting frustrating...

  • Resizing ListCellRenderers using JTextArea and word wrap

    Hi,
    I'm trying to create a ListCellRenderer to draw nice cells for a JList. Each cell has a one line title and a description of variable length. So, I create a custom ListCellRenderer, which consists of a JPanel containing a JLabel for the title and a JTextArea for the description. I set the JTextArea to do wrapping on word boundried and it all almost works.
    But, the list cells don't resize to accomodate the variable height of the cells. They show only 1 line for the title and 1 line for the description.
    I have seen a few threads relating to this including the article, "Multi-line cells in JTable in JDK 1.4+" in the Java Specialists' Newsletter, which seems to do a similar trick for JTables. But so far nothing I've tried works.
    Curiously, using println debugging, my getListCellRenderer method seems to get called for each cell twice while my GUI is being drawn. It looks to me like the TextUI view that draws the JTextArea seems to do the word wrapping after the first set of getListCellRenderer calls. But, it's the first set of calls that seems to establish the layout of the list.
    Any help would be appreciated. I've pulled out quite enough hair over this one.
    -chris
    package bogus;
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Font;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    public class CellRendererTest {
         public static void main(String[] args) {
              final CellRendererTest test = new CellRendererTest();
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        test.createGui();
         public void createGui() {
              JFrame frame = new JFrame("Testing Bogus ListCellRenderer");
              JList list = new JList(createBogusListItems());
              list.setCellRenderer(new MyListCellRenderer());
              frame.add(list);
              frame.setSize(200, 400);
              frame.setVisible(true);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         private MyListItem[] createBogusListItems() {
              List<MyListItem> items = new ArrayList<MyListItem>();
              items.add(new MyListItem("First item", "This is a nice short description."));
              items.add(new MyListItem("Another one", "This is a longer description which might take up a couple of lines."));
              items.add(new MyListItem("Next one", "This is a ridiculously long description which is total gibberish." +
                        "Blah blah blabber jabber goo. Blither blather bonk. Oink boggle gaggle ker-plunk."));
              items.add(new MyListItem("No Desc", null));
              items.add(new MyListItem("Last one", "Boink!"));
              return items.toArray(new MyListItem[items.size()]);
         static class MyListCellRenderer extends JPanel implements ListCellRenderer {
              private JLabel title;
              private JTextArea desc;
              // hack to see if this is even possible
              private static int[] rows = {1, 2, 3, 0, 1};
              public MyListCellRenderer() {
                   setLayout(new BorderLayout());
                   setBorder(BorderFactory.createEmptyBorder(6, 4, 6, 4));
                   setOpaque(true);
                   title = new JLabel();
                   title.setFont(new Font("Arial", Font.ITALIC | Font.BOLD, 11));
                   add(title, BorderLayout.NORTH);
                   desc = new JTextArea();
                   desc.setFont(new Font("Arial", Font.PLAIN, 9));
                   desc.setOpaque(false);
                   desc.setWrapStyleWord(true);
                   desc.setLineWrap(true);
                   add(desc, BorderLayout.CENTER);
              public Component getListCellRendererComponent(JList list, Object value,
                        int index, boolean isSelected, boolean cellHasFocus) {
                   MyListItem item = (MyListItem)value;
                   title.setText(item.title);
                   if (item.description != null && item.description.length() > 0) {
                        desc.setText(item.description);
                        desc.setVisible(true);
                   else {
                        desc.setVisible(false);
                   // uncomment next line to to somewhat simulate the effect I want (hacked using the rows array)
                   // desc.setRows(rows[index]);
                   if (isSelected) {
                        setBackground(list.getSelectionBackground());
                        setForeground(list.getSelectionForeground());
                   else {
                        setBackground(list.getBackground());
                        setForeground(list.getForeground());
                   return this;
         static class MyListItem {
              String title;
              String description;
              public MyListItem(String title, String description) {
                   this.title = title;
                   this.description = description;
    }

    This seems to work
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Insets;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.text.View;
    public class CellRendererTest {
         int WIDTH = 220;
         public static void main(String[] args) {
              final CellRendererTest test = new CellRendererTest();
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        test.createGui();
         public void createGui() {
              JFrame frame = new JFrame("Testing Bogus ListCellRenderer");
              JList list = new JList(createBogusListItems());
              final MyListCellRenderer renderer = new MyListCellRenderer();
              list.setCellRenderer( renderer );
              JScrollPane listScrollPane = new JScrollPane(list);
              frame.add(listScrollPane);
              frame.setSize(WIDTH, 400);
              frame.setVisible(true);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         private MyListItem[] createBogusListItems() {
              List<MyListItem> items = new ArrayList<MyListItem>();
              items.add(new MyListItem("First item", "This is a nice short description."));
              items.add(new MyListItem("Another one", "This is a longer description which might take up a couple of lines."));
              items.add(new MyListItem("Next one", "This is a ridiculously long description which is total gibberish." +
                        "Blah blah blabber jabber goo. Blither blather bonk. Oink boggle gaggle ker-plunk."));
              items.add(new MyListItem("No Desc", null));
              items.add(new MyListItem("Last one", "Boink!"));
              items.add(new MyListItem("Ooops this one breaks things by being longer than my fixed width", "Blabber babble gibber flipper flop."));
              return items.toArray(new MyListItem[items.size()]);
         static class MyListCellRenderer extends JPanel implements ListCellRenderer {
              public JLabel title;
              public JTextArea desc;
              Color altColor = new Color(0xeeeeee);
              public MyListCellRenderer() {
                   setLayout(new BorderLayout());
                   setBorder(BorderFactory.createEmptyBorder(6, 4, 6, 4));
                   setOpaque(true);
                   title = new JLabel();
                   title.setFont(new Font("Arial", Font.ITALIC | Font.BOLD, 11));
                   add(title, BorderLayout.NORTH);
                   desc = new JTextArea();
                   desc.setFont(new Font("Arial", Font.PLAIN, 9));
                   desc.setOpaque(false);
                   desc.setWrapStyleWord(true);
                   desc.setLineWrap(true);
                   add(desc, BorderLayout.CENTER);
              public Component getListCellRendererComponent(JList list, Object value,
                        int index, boolean isSelected, boolean cellHasFocus) {
                   Insets insets = desc.getInsets();
                int rendererLeftRightInsets = insets.left + insets.right + 8;  // 8 from panel border
                int topDownInsets = insets.top + insets.bottom;
                int listWidth = list.getWidth();
                int viewWidth = listWidth;
                   int scrollPaneLeftRightInsets = 0;
                   JScrollPane scroll = (JScrollPane) SwingUtilities.getAncestorOfClass( JScrollPane.class, list );
                   if ( scroll != null && scroll.getViewport().getView() == list ) {
                        Insets scrollPaneInsets = scroll.getBorder().getBorderInsets(scroll);
                     scrollPaneLeftRightInsets = scrollPaneInsets.left + scrollPaneInsets.right;
                     listWidth = scroll.getWidth() - scrollPaneLeftRightInsets;
                     JScrollBar verticalScrollBar = scroll.getVerticalScrollBar();
                     if (verticalScrollBar.isShowing()) {
                         listWidth -= verticalScrollBar.getWidth();
                     viewWidth = listWidth - rendererLeftRightInsets;
                   MyListItem item = (MyListItem)value;
                   title.setText(item.title);
                   if (item.description != null && item.description.length() > 0) {
                        desc.setText(item.description);
                        desc.setVisible(true);
                        View rootView = desc.getUI().getRootView(desc);
                     rootView.setSize( viewWidth, Float.MAX_VALUE );
                    float yAxisSpan = rootView.getPreferredSpan(View.Y_AXIS);
                        Dimension preferredSize = new Dimension( viewWidth, (int)yAxisSpan + topDownInsets );
                        desc.setPreferredSize( preferredSize );
                   } else {
                        desc.setVisible(false);
                   title.setPreferredSize( new Dimension( viewWidth, title.getPreferredSize().height ) );
                   // uncomment next line to to somewhat simulate the effect I want (hacked using the rows array)
                   //desc.setRows(rows[index]);
                   if (isSelected) {
                        setBackground(list.getSelectionBackground());
                        setForeground(list.getSelectionForeground());
                   else {
                        if (index % 2 == 0)
                             setBackground(altColor);
                        else
                             setBackground(list.getBackground());
                        setForeground(list.getForeground());
                   return this;
         static class MyListItem {
              String title;
              String description;
              public MyListItem(String title, String description) {
                   this.title = title;
                   this.description = description;
    }

  • Word wrap - add return within cell

    I have the need of putting word(s) on definitive lines within a cell. The particular case I am working with at the moment is a cemetery listing that has additional information particularly multiple spouses. I want each spouse's name to appear on a different line within that cell which Xcel does by using either ctl/enter or option/enter rather than putting a number of spaces in a wrapped cell so the next name would shift to the next line. Does Numbers have something similar?
    Tks

    Found another neat feature that also works with word wrap. Use Inspector to Merge two or more cells, I happen to have a long text in the first cell of three cell I wanted to use. Follow that by adding Word Wrap while still in Inspector. Low and Behold, the text that far exceeded the one cell now uses three cell for display and if the data then exceeds that amount of space it wraps as if the three cell are a single cell. It just so happened that the data I mentioned above needed three lines. Also, if you want specific parts of the data to be on specific lines within the unified cell, both line break functions previously mentioned will force the next part of the data to go to the next line ad infintum(??) or until the combined cell space/character limit is reached.

  • Word wrap on JTextArea

    Dear all,
    I have a created a little GUI with a menu. I can select from menu to load a text file and display it on the JTextArea. Is it possible to do "word wrap" on the JTextArea instead of having a schroller???
    thx in advance
    vxc

    setLineWrap(true);
    setWrapStyleWord(true);

  • Word wrap in JTextArea

    Dear all,
    I have a created a little GUI with a menu. I can select from menu to load a text file and display it on the JTextArea. Is it possible to do "word wrap" on the JTextArea instead of having a schroller???
    thx in advance
    vxc

    Certainly,
              // limit the area's width - or rely on the layout manager to do this
              JTextArea txt = new JTextArea(10, 20);
              // set it to word wrap without breaking individual words           
              txt.setLineWrap(true);
              txt.setWrapStyleWord(true);Dom.

  • JTextArea word wrapping

    I know I can use a method to word wrap a JTextArea, but is there any simple way so that word wrapping does not cut words in half in order to fit?
    In other words, the current word wrapping will break a word at any position once the max line length has been met, so a word like "extension" may be broken up into "exte" and "nsion" on the next line.

    Straight from the API:
    setWrapStyleWord(...);

  • JTextArea Word Wrap Frustrations

    Howdy.
    I am developping with Borland JBuilder 2. Each time I try to use the method JTextArea.setWrapStyleWord(true) I get this error:
    Error: (70) method setWrapStyleWord(boolean) not found in class com.sun.java.swing.JTextArea.
    Any suggestions? Do I need a newer version of the Swing classes? My setLineWrap(true) method works perfectly. Is there any other way to do word wrapping on a JTextArea in a JScrollPane?
    Thanks much!
    ~Matt

    set the wrap style for te specific object you created,
    don't try to set it for the class itself. (i.e.
    JTextArea.setWrapStyleWord(true))I don't really think that zhentilar5 tried to do what you he did. Besides the error message would be a BIT different, wouldn't it? This is not a static method.
    zhentilar5 I believe that there is a compatibility issue here indeed. According to my best recollection, I cannot remember what version you need... :) Try to use the setLineWrap(boolean) instead.
    Hope that helped.
    afotoglidis

  • How to make items in a list word wrap as needed and be variable heights

    I am trying to build a custom itemrenderer for the List control.  The items in the list are variable lengths of text, some of the text items will have different colors determined at runtime base on some criteria (this works fine now with my custom itemrenderer).  What I need is for the items to word wrap, and therefore for the list to display items of varying heights.  Is this possible to do?  All my attempts seem to have failed.  If I can't do this with an item renderer any suggestions about how to do this?  Thanks very much in advance to any of you gurus who are able to help me.

    Trick here is not to specify any height for the renderer so that Flex will determine the height according to the content. Also set the variableRowHeight="true"
    Here is a simple example
    <mx:ArrayCollection id="dp">
            <mx:Array>
                <mx:Object label="some very long text"/>
                <mx:Object label="some very loooooooooooooooooooooooooooooooooong text"/>
                <mx:Object label="some very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text"/>
                <mx:Object label="some very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text"/>
                <mx:Object label="some very long text"/>
            </mx:Array>
        </mx:ArrayCollection>
        <mx:List dataProvider="{dp}" variableRowHeight="true">
            <mx:itemRenderer>
                <mx:Component>
                    <mx:Canvas width="100%">
                        <mx:Text width="100%" text="{data.label}"/>
                    </mx:Canvas>
                </mx:Component>
            </mx:itemRenderer>
        </mx:List>

  • 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 Wrapping JLabel

    I'm having trouble getting JLabel's to word wrap. I've tried using html tags. Is there any way I ca do this? What I'm trying to do is read in from a file, get Questions and Answers, and display quesitons and answers to a JLabel using setText(). The dimensions are (400, 450) but even with html tags on the text I'm setting, it just runs right off the panel.
    Any suggestions would be much appreciated.
    Thanks,

    Below is my JTextArea. As you can see I have it set for 2 rows and 40 col. When I run the program, it's still just one line of text straight off my panel.
    Do you see anything I'm missing here?
                quesJTextArea = new JTextArea("",2,40);
                quesJTextArea.addMouseListener(this);
                quesJTextArea.setLineWrap(true);
                quesJTextArea.setWrapStyleWord(true);
                quesJTextArea.setEditable(false);
                quesJTextArea.setBackground(Color.gray);

  • 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

  • Word Wrap

    Hi Guys
    I neec to use word wrap in JTable cells but can't figure-out how to do it; can any of you help me?
    Many thanks
    Patrick

    Many thanks for your replies.
    The JTextArea works fine but the table rows do not resize when the columns are resized so we end-up with the text being displayed on a single line and a lot of white-space below. How can I get the table rows to resize to the height og the tallest cell?
    Many thanks for your help.

Maybe you are looking for