This is irritating me so bad, I have spent three hours aimlessly trying to get JScrollPane to work on my JList and I can't figure it out.. here is the code, please make the scroll bars work! :sad:

public class ChatServer extends JPanel {

    private JList lstUsers;
    private JScrollPane scroller;

    public ChatServer() {

        lstUsers = new JList (lstUsersItems);
        scroller = new JScrollPane(lstUsers);

        setPreferredSize (new Dimension (646, 556));
        setLayout (null);

        add (lstUsers);

        lstUsers.setBounds (240, 35, 200, 230);

        lstUsers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        add(scrollPane, BorderLayout.CENTER);

        }
}

I have no idea, and after several tutorials and many many syntax changes, it still doesn't work.. Sometimes the JList won't even appear on my form, sometimes it will without a scroll bar and will be uneditable.. The rest of teh code is irrelivant, but it is a chatServer I am writing.. Comming along wonderfully, except for this one little thing.. :cry:

Dani AI

Generated

Good catch from on layouts. Two issues in your snippet are blocking scrollbars: (1) the JList is being added directly to the panel instead of only to the JScrollPane, and (2) the panel uses a null layout so your BorderLayout constraint is ignored. Also note the variable mismatch: you create scroller but call add(scrollPane, ...). A Swing component can have only one parent; if you add the list to the panel, it gets pulled out of the scrollpane and the viewport ends up empty.

Recommended approach: let a layout manager size the scrollpane and add only the scrollpane to the container.

// Panel constructor
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> list = new JList<>(model);
list.setVisibleRowCount(12);                  // when items > 12, vertical bar appears

JScrollPane sp = new JScrollPane();
sp.setViewportView(list);
// sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); // optional

setLayout(new BorderLayout());
add(sp, BorderLayout.CENTER);

If you truly must position things absolutely, at least give bounds to the scrollpane (not the list), and never add the list directly to the panel:

JList<String> list = new JList<>(someArrayOrModel);
JScrollPane sp = new JScrollPane();
sp.setViewportView(list);

setLayout(null);
add(sp);                   // do NOT add(list)
sp.setBounds(240, 35, 200, 230);

Practical checks:

  • Add items to the model so there is something to scroll.
  • Only one add: add the scrollpane, not the list.
  • Use the same variable you created (scroller vs scrollPane).
  • Prefer a real layout (BorderLayout, BoxLayout, GridBagLayout) over absolute positioning so your UI resizes cleanly across platforms, echoing ’s point.

Recommended Answers

All 3 Replies

Never use a null layout!

jList1 = new JList(data);
    jScrollPane1.getViewport().add(jList1);

This should work. You might want to add a minimum size as well to prevent the list from being made smaller and smaller when there's few items in it (until it disappears when there are no items).

jList1.setMinimumSize(new Dimension(100,100));

YAY! :mrgreen: it works! that took a large chunck out of my life, but finally I came up with the following code:

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

public class ScrollBars extends JPanel {
    private JList lst;
    private JScrollPane JSP;

    public ScrollBars() {
        //construct preComponents
        String[] lstItems = {"Item 1", "Item 2", "Item 3", "sdaf", "sadfasd", "asdf"};

        //construct components
        lst = new JList (lstItems);
        JSP = new JScrollPane(lst);

		lst.setMinimumSize(new Dimension(150,100));

        //adjust size and set layout
        setPreferredSize (new Dimension (329, 124));
        setLayout (null);

        //add components
        add (lst);
        add (JSP);

        JSP.getViewport().add(lst);

        //set component bounds (only needed by Absolute Positioning)
        JSP.setBounds (165, 15, 150, 100);
        lst.setBounds (165, 15, 150, 100);

    }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("ScrollBars");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new ScrollBars());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible (true);
    }
}

Oh, and I have to set the Layout to null in order to do absolute positioning! If this is wrong, tell me what to put there to enable absolute positioning like I have it.. The GUI code was generated by GUI Genie and it put that line there.. Unfortunatelty, GUI Genie sucks and it doesn't do JScrollPane... So if you know of a better FREE GUI editor let me know..

Absolute positioning is wrong. It's there for emergency use but defeats the very purpose of using a platform independent user interface library.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.