MD. NASHID KAMAL MD. NASHID KAMAL - 4 days ago 4
Java Question

JScrollPane not working in my code

Here, My Jscrollpane isn't working. I have added the scrollpane and list both on main Jpanel. The Jlist is working, But it seems, JScrollpane Not working in this code.

my main class extends Jframe, so from main class i just pass the main panel, and working on the same panel.
and here i'm using absolute layout.

public class ExternalsLinks extends JPanel {

///Links List
private JList mainList;

///List Custom Border
Border lineBorder = BorderFactory.createLineBorder(Color.BLACK, 5);

///Main Jpanel
private JPanel mainPanel;

///Scroll Pane for Lists
private JScrollPane mainListScrollPane;

/// Lebels
private JLabel lblExternalLinks;

///Buttons
private Button btnBackCredits = new Button("Back");

///Button Properties
private final int BUTTON_X = 220; private final int BUTTON_Y = 450;
private final int BUTTON_X_LENGTH = 350; private final int BUTTON_Y_LENGTH = 50;

///List Options
private String [] listMenu = {"Searching", "Sorting"};

///Constructors
public ExternalsLinks(JPanel mainPanel) {

this.mainPanel = mainPanel;
this.mainPanel.setLayout(null);

mainPanel.setBackground(Color.WHITE);

initialize_All();
}

/// Initialize method to initialize all components to JFrame
private void initialize_All() {

///Main Panel Components

lblExternalLinks = new JLabel("\"External Links\"");
lblExternalLinks.setBounds(290, 0, 400, 100);
lblExternalLinks.setFont(new Font("courier", Font.BOLD, 30));
mainPanel.add(lblExternalLinks);

mainList = new JList(listMenu);
mainList.setBackground(Color.CYAN);
mainList.setForeground(Color.BLACK);
mainList.setFont(new Font("Calibiri", Font.BOLD, 25));

mainList.setVisibleRowCount(2);
mainList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

mainList.setBorder(lineBorder);
mainList.setBounds(50, 80, 300, 350);

mainListScrollPane = new JScrollPane();
mainPanel.add(mainListScrollPane);
mainPanel.add(mainList);





///Buttons
btnBackCredits.setBounds(BUTTON_X, BUTTON_Y, BUTTON_X_LENGTH, BUTTON_Y_LENGTH);
mainPanel.add(btnBackCredits);

btnBackCredits.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
mainList.setVisible(false);

mainPanel.removeAll();
new MainMenu(mainPanel);
}
});
}
}

Answer

I have added the scrollpane and list both on main Jpanel.

That's a problem -- don't do that. You can only add a component to one container, and the JScrollPane counts as a container. So add your JList to the JScrollPane's viewport, and then only add the JScrollPane to the GUI.

i'm using absolute layout

And that's another serious problem. Don't use null layouts and setBounds(...). For instance calling setBounds on your list will prevent the scrollpane from being able to work since the list can't expand inside the JScrollPane as it should. Learn and use the layout managers.

Also, you never add the JList to the JScrollPane's viewport!

You need JScrollPane scrollPane = new JScrollPane(myList); or something similar, with whatever your variable names are. Please go through the tutorials as this is all fully explained there.

For example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;

@SuppressWarnings("serial")
public class ExternalsLinks2 extends JPanel {

    // constants
    private static final String[] LIST_DATA = {"Searching", "Sorting"};
    private static final Font LIST_FONT = new Font("Calibiri", Font.BOLD, 25);
    private static final Font LABEL_FONT = new Font("courier", Font.BOLD, 30);
    private static final int LIST_VISIBLE_ROW_COUNT = 10;
    private static final String TITLE_TEXT = "External Links";
    private static final Color LIST_BG = Color.CYAN;

    // JList field created with constant array data
    private JList<String> jList = new JList<>(LIST_DATA); 

    public ExternalsLinks2() {
        jList.setFont(LIST_FONT);
        jList.setPrototypeCellValue("ABCDEFGHIJKLMNOP ABCDE");
        jList.setVisibleRowCount(LIST_VISIBLE_ROW_COUNT);
        jList.setBackground(LIST_BG);

        JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
        titleLabel.setFont(LABEL_FONT);

        JButton backButton = new JButton("Back");
        // add ActionListener or AbstractAction here

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new BorderLayout(5, 5));  // use BorderLayout
        add(titleLabel, BorderLayout.PAGE_START);  // JLabel at top
        add(new JScrollPane(jList), BorderLayout.CENTER);  // JList inside of JScrollPane in center
        add(backButton, BorderLayout.PAGE_END);  // JButton at bottom
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("External Links");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ExternalsLinks2());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Which displays as:

enter image description here

Comments