0

I don't know if this is just when I'm using the the DefaultListModel instead of a vector, but the issue is that when I make my JList & JScrollPane, the scrollbars show correctly but i cannot scroll even though there are enough elements to fill the full window.

Source code:

panel_unit.add(YUi.JScrollPane(list = YUi.JList(main.config.fdata,0,0,this,0),500,314,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS),5,5);
public static JList JList(DefaultListModel text,int width,int height,ListSelectionListener listener,int SelIndex) {
    JList list = new JList(text);
    list.setPreferredSize(new Dimension(width,height));
    list.setSelectedIndex(SelIndex);
    list.addListSelectionListener(listener);
    return list;
}
public static JScrollPane JScrollPane(Component text,int width,int height,int HorizontalScrollBarPolicy,int VerticalScrollBarPolicy) {
    JScrollPane scrollpane = new JScrollPane(text);
    scrollpane.setPreferredSize(new Dimension(width,height));
    scrollpane.setHorizontalScrollBarPolicy(HorizontalScrollBarPolicy);
    scrollpane.setVerticalScrollBarPolicy(VerticalScrollBarPolicy);
    return scrollpane;
}
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
Olindholm
  • 340
  • 4
  • 16
  • 2
    don't ever use setXXSize (XX == min, pref, max) – kleopatra Jan 19 '12 at 17:10
  • setPrefferedSize have nvr let me down so why shouldn't I use it? – Olindholm Jan 19 '12 at 17:34
  • some reasons: http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519 .. plus it's most probably related to the problem you are experiencing here ;-) – kleopatra Jan 20 '12 at 13:05

3 Answers3

4

It probably has to do with your use of setting the preferred size, and using JScrollPane.VERTICAL_SCROLLBAR_ALWAYS. If you put that constant there it will always show a scrollbar regardless of if you can scroll or not.

Just try and I bet it will work:

new JScrollPane( list );
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • Nope, if i remove the things about the scrollbars the vertical scrollbar doesn't even show up – Olindholm Jan 19 '12 at 17:32
  • 2
    You are setting the preferred size on the list. Remove any setPreferredSize() on the list, and it will work. Setting the preferredSize causes the list to always be that width height regardless of the content, and the scrollpane always sets itself as the preferred size of the list. If the list says I want to be 1000 pixels then the scroll bar will allow it to be 1000 pixels, and show a scroll bar to access the portions not displayed. Otherwise the scrollbar doesn't "see" the undisplayed parts. – chubbsondubs Jan 19 '12 at 17:33
1

You should wrap your JScrollPane around the JList

Example:

JScrollPane scrollpane = new JScrollPane(list);

PS: The naming conventions you are using are not appropriate at all!!!

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
1

Steps are :

  1. Create the JList component , called let's say "myJList" .

  2. Create the JScrollPane component, using your JListComponent : JScrollPane myScrollPane = new JScrollPane(myJList);

  3. Add only the JScrollPane to the GUI : .add(myScrollPane)