1

I am using a JList to wrap some variable read-only text displayed to the user. Each line of text is rendered in a list cell based on JTextArea, i.e.

public class InfoTextCellRenderer extends JTextArea implements ListCellRenderer

and this renderer is used by the list:

textList.setCellRenderer(new InfoTextCellRenderer());

The JList is embedded in a JScrollPane.

I do all this because the typical usage pattern here is to use the arrow keys, not the mouse. This allows the current cell to be highlighted, making it obvious to the user what is happening. The previous implementation, using a single JTextArea, required the user to press down arrow multiple times until the invisible caret (invisible because it wasn't editable) reached the point where scrolling could occur (bottom or top of window).

The problem is that even though the InfoTextCellRenderer turns line wrap and word wrap on, when the text is displayed the wrapping doesn't happen.

public class InfoTextCellRenderer extends JTextArea implements ListCellRenderer {

    public InfoTextCellRenderer() {
        this.setFocusable(false);
    }

    /* (non-Javadoc)
     * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
     */
    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {    
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setEditable(false);
        setFont(list.getFont());
        this.setLineWrap(true);          
        this.setWrapStyleWord(true);
        setText(value == null ? "" : value.toString());
        return this;
    }

}

What might be preventing the line wrapping from occurring?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Steve Cohen
  • 4,679
  • 9
  • 51
  • 89
  • possible duplicate of [Swing JList with multiline text and dynamic height](http://stackoverflow.com/questions/7306295/swing-jlist-with-multiline-text-and-dynamic-height) – Aboutblank Jul 04 '13 at 01:04

1 Answers1

1

Found my answer at this other Stack Overflow post though I wasn't immediately clear of its relevance to my situation: https://stackoverflow.com/questions/7306295/swing-jlist-with-multiline-text-and-dynamic-height?rq=1

Community
  • 1
  • 1
Steve Cohen
  • 4,679
  • 9
  • 51
  • 89