简体   繁体   中英

JList scroll bug?

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;
}

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 );

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!!!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM