Reputation: 4859
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?
Upvotes: 1
Views: 1714
Reputation: 4859
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
Upvotes: 1