javaswingjscrollpanejlistjscrollbar

scroll bar doesn't appear on JList


I'm using a GridBagLayout and have a JList of my custom class. The JList itself is shown as I expect, but the scroll bar doesn't appear. (My size of data is also larger than the size of my JList.) I checked many of related links and all of them mentioned these 2 lines to do so, but it didn't work for me.

JScrollPane scrollPane = new JScrollPane(list); 

add(scrollPane);

Full code:

import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import javax.swing.*;

public class UserPart extends JFrame {
    Label messageL = new Label("Confirm an item for more options.");
    Button confirmB = new Button("Confirm");
    Button backB = new Button("Back");
    DefaultListModel<User2> model;
    public UserPart() {

        JList<User2> list = new JList<User2>();
        model = new DefaultListModel<User2>();
        for (int i = 0; i < MainActivity2.arr.size(); i++) {
            User2 u = new User2();
            u.setForShow(i);
            model.addElement(u);
        }
        // SETTING THE APPEARANCE OF THE JLIST
        list.setModel(model);
        list.setCellRenderer(new MyListCellRenderer());
        list.setLayoutOrientation(JList.VERTICAL);
        list.setPreferredSize(new Dimension(500, 750));
        //////////////////////////////////////////////    
        GridBagLayout grid = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(grid);
        setTitle("List of users");
        GridBagLayout layout = new GridBagLayout();
        this.setLayout(layout);

        gbc.gridx = 0;
        gbc.gridy = 0;
        this.add(messageL, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        this.add(list, gbc); //ADDED JLIST TO LAYOUT HERE
        JScrollPane scrollPane = new JScrollPane(list); // DEFINED SCROLL BAR HERE
        add(scrollPane); // ADDED SCROLL BAR TO LAYOUT HERE
        setSize(1000, 1500);
        setPreferredSize(getSize());
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Solution

  • Set the constraints weight (weightx, weighty) to a value greater than zero - to use all the layout - also set fill to BOTH. Do not add the list directly to the frame, just add the scroll pane with the constraints set as explained above. Do not mix AWT components (Label) with Swing ones, use JLabel instead. Here the summary:

    JLabel label = new JLabel("Confirm an item for more options.");
    
    JScrollPane scroll = new JScrollPane(list);
    
    GridBagConstraints gbc = new GridBagConstraints();
    
    JFrame frame = new JFrame();
    frame.setLayout(new GridBagLayout());
    gbc.gridx = 0;
    gbc.gridy = 0;
    frame.add(label, gbc);
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH;
    frame.add(scroll, gbc);