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:

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.