KnowledgeGeek
KnowledgeGeek

Reputation: 197

Adding JScrollPane to my JList

I am having issues getting a vertical scrollbar on my jlist.

listInfo is a vector with data in it. right panel is a panel on the EAST side of the frame.

If I add just the JList, it appears. If I add scrollpane, nothing happens.

        data = new JList(listInfor);
        data.setVisible(true);
        data.setVisibleRowCount(5);
        data.setPreferredSize(new Dimension(400,400));
        data.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JScrollPane scroll = new JScrollPane(data);
        rightPanel.add(scroll);

For some reason, this isn't working.

Upvotes: 0

Views: 106

Answers (3)

afsantos
afsantos

Reputation: 5206

You should specify a layout manager for your rightPanel variable, if it hasn't any. Absence of a layout manager may give you unexpected results, as you don't set the bounds of your components manually.

Besides, as pointed out by MadProgrammer's answer, you shouldn't set preferred sizes on your components, if possible. There are other ways to determine appealing dimensions. For instance, with JList, using setVisibleRowCount.

In order for the scroll to appear only as needed, you can specify the scroll bar policies on the JScrollPane constructor, or use its set methods to do so. Example:

jlist.setVisibleRowCount(5);
JScrollPane scroll = new JScrollPane(jlist);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

Also, make sure the container of the scroll pane respects its preferred size, as the scroll pane respects the list's preferred size.

Upvotes: 0

user2154826
user2154826

Reputation:

Tested this

public static void main(String[] args) {

            String[] ar = {"one", "two", "three"};
            JList data = new JList(ar);

            data.setVisible(true);
            data.setVisibleRowCount(5);
            data.setPreferredSize(new Dimension(400,400));
            data.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            JScrollPane scroll = new JScrollPane(data);

            JFrame frame = new JFrame("StackOverflow Test");
            frame.add(scroll);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack(); 
            frame.setLocationRelativeTo(null);  // to center
            frame.setVisible(true);
        }

and all it's okay

enter image description here

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

Don't set the preferred size of the JList, this is preventing it from calculating the size of the list based on it's contents, which will mean that it will never exceed the preferred size you have supplied, meaning that it will never show any scroll bars.

Instead, use a combination of JList#setVisibleRowCount and Jlist#setPrototypeCellValue to adjust how the JList calculates it's preferred size.

IF you REALLY want more control, you should take a look at the Scrollable interface that JList implements, but even then, I'd be careful

Upvotes: 3

Related Questions