Search code examples
javaswingjscrollpanejlist

JList view area grows despite adding a JScrollPane


Here is my code:

protected JComponent createCommandHistory() {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    listModel = new DefaultListModel();
    list = new JList(listModel);

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.addListSelectionListener(this);
    list.setVisibleRowCount(10);

    JScrollPane listScrollPane = new JScrollPane(list);

    panel.add(list);
    panel.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH);
    panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

    return panel;   
}

As elements are added to the list, the view area grows instead of being limited by a scroller.

Any help is appreciated! Thanks


Solution

  • panel.add(list);
    

    Should be:

    panel.add(listScrollPane);