I have a JFrame which consists only of a JList, the JList has its own cell renderer, in the cell renderer a JTextPane is returned with a custom size. The custom size has the result that the lines of this text panes are wrapped.
textPane.setSize(WIDTH, HEIGHT);
Now I want to make this JList scrollable, so I wrap it into a JScrollPane component, this however, nullifies the width constraints I have defined in the cell renderer. What would be the best way to enforce the width constraint in the JScrollPane?
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLayout(new BorderLayout());
DefaultListModel<Status> listModel = new DefaultListModel<Status>();
JList<Status> list = new JList<>(listModel);
list.setLayout(new BorderLayout());
list.setSize(FRAME_WIDTH, FRAME_HEIGHT);
list.setCellRenderer(new MyCellRenderer());
// wrap the list with a JScrollPane
add(new JScrollPane(list), BorderLayout.CENTER);
The render operation of
Cellis done byjavax.swing.plaf.basic.BasicListUIclass, and it will paint whole case, no matter what size you set for cell renderer. You can dig into the source code ofBasicListUIclass to understand how it works.I guess you just to make sure the content of the cell can be all displayed. In this case, you can add a horizontal scroll bar for the list. To do this, get a preferred width for the list.
Like below: