Frage

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

War es hilfreich?

Lösung

panel.add(list);

Should be:

panel.add(listScrollPane);

Andere Tipps

You're adding the list to the panel - instead, you need to add the listScrollPane.

In addition, when adding a component to a JPanel that has a BorderLayout, be sure to specify the position - e.g., BorderLayout.CENTER.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top