1

code:

list1items = new DefaultListModel();
list1items.addElement("-");
list1 = new JList(list1items);
list1.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
list1.setBounds(0,0, 100,100);
JScrollPane list1scr = new JScrollPane(list1);
list1scr.setPreferredSize(new Dimension(20, 20));
list1.setVisibleRowCount(8);
getContentPane().add (list1scr);

And no scroll-bar appears. When there are too many items, they are hidden, I cant reach them. How to solve this?

John Smith
  • 6,129
  • 12
  • 68
  • 123
  • 2
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) `list1.setBounds(0,0, 100,100);` Don't do that. Use layouts. 3) See [`JList.setVisibleRowCount(int)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html#setVisibleRowCount%28int%29). 4) `getContentPane().add (list1scr);` – Andrew Thompson Dec 20 '12 at 14:16
  • thx, but those wont work either. I update the question – John Smith Dec 20 '12 at 15:29
  • *"those wont work either."* The 1st will. Try it. – Andrew Thompson Dec 20 '12 at 16:04

2 Answers2

10
getContentPane().add(list1scr);
Michael 'Maik' Ardan
  • 4,213
  • 9
  • 37
  • 60
  • 3
    +1 good catch :-) But best to delete your comment _quickly_ - the general rule is to never-ever do any manual sizing/positioning of components ever – kleopatra Dec 20 '12 at 14:23
  • @kleopatra For the love of God, can someone tell me what would be consequences of calling setXXXSize() methods? – Branislav Lazic Dec 20 '12 at 14:39
  • Thanks @kleo. Anyway, why is that? – Michael 'Maik' Ardan Dec 20 '12 at 14:44
  • 3
    @brano88 every `JComponents` can returns its own `PreferredSize`, `JList` has methods for visible rows, in contrast with empty `JPanel` / `JComponent` (`Painting in Swing` doeasn't returns any `Dimension`, `JPanel` is empty for `Standard LayoutManagers`) then there have to override `PreferredSize` – mKorbel Dec 20 '12 at 15:06
  • and whats the matter with absolute poisitoning? You think I will suck with arranging it to east, west, after west GAAAAAAAAHHHH – John Smith Dec 20 '12 at 15:28
  • 4
    @brano88: [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing](http://stackoverflow.com/q/7229226/230513)? – trashgod Dec 20 '12 at 15:29
  • @JohnSmith: [*Absolute Positioning*](http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html) is not forbidden, but it scales poorly in both geometry and maintenance. – trashgod Dec 20 '12 at 15:34
  • 3
    Please post SSCCE for further assistance. – Michael 'Maik' Ardan Dec 20 '12 at 15:35
  • @mKorbel Thanks for clarifying that, and sorry for posting my question here. I mean...I do avoid to call setXXXSize methods, but I never encountered any problems when it comes to functionality of my applications. – Branislav Lazic Dec 20 '12 at 15:37
5

To expand on Michael Ardan's answer, you were adding you JList to the panel instead of the JScrollPane. The JScrollPane must be added to the panel and the JList must be added to the ScrollPane for it to work. There's really no need to use setBounds or setPreferredSize - get rid of them. JList takes care of all that when you call the setVisibleRowCount method. Here's an example of your ScrollPane working. If you still have problems, plug your own code into this example until it breaks. Then tell us what broke it. If not, accept Michael's answer.

import java.awt.*;
import javax.swing.*;

public class Temp extends JPanel{
    public Temp(){

        DefaultListModel list1items = new DefaultListModel();
        list1items.addElement("-");
        for(int i = 0; i < 200; i++)
            list1items.addElement("Item " + i);
        JList list1 = new JList(list1items);
        list1.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
        JScrollPane list1scr = new JScrollPane(list1);
        list1.setVisibleRowCount(8);
        add (list1scr);
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Temp());
        frame.pack();
        frame.setVisible(true);
    }
}
Nick Rippe
  • 6,465
  • 14
  • 30