Search code examples
javalistswingjscrollpanejlist

Why can't I see the JList?


Main Class

public class Main {

    public static void main(String[] args) {
        new CredentialManager();
    }
}

Main Window Class

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

public class CredentialManager extends JFrame implements ActionListener {

    public CredentialManager() {

        View.credentialManager.setSize(1200, 800);
        View.credentialManager.setResizable(false);
        View.credentialManager.setLayout(null);
        View.credentialManager.setLocationRelativeTo(null);
        View.credentialManager.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        View.credentialManager.setVisible(true);
        View.btnCredentialManagerManageUser.setBounds(550, 50, 120, 30);
        View.btnCredentialManagerSignOut.setBounds(50, 50, 95, 30);
        View.listCredentials.setBounds(100,100, 75,75);
        View.btnCredentialManagerManageUser.addActionListener(this);
        View.btnCredentialManagerSignOut.addActionListener(this);
        View.credentialManager.add(View.btnCredentialManagerManageUser);
        View.credentialManager.add(View.btnCredentialManagerSignOut);

        View.credentialManager.add(View.scrollPaneCredentials);
        View.scrollPaneCredentials.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        View.listModelCredentials.addElement("Credential1");
        View.listModelCredentials.addElement("Credential2");
        View.listModelCredentials.addElement("Credential3");
        View.listModelCredentials.addElement("Credential4");
        View.listModelCredentials.addElement("Credential5");
        View.listModelCredentials.addElement("Credential6");

    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if (actionEvent.getSource().equals(View.btnCredentialManagerManageUser)) {
            System.out.println("Manager");
        } else if (actionEvent.getSource().equals(View.btnCredentialManagerSignOut)) {
            System.out.println("Sign Out");

        }
    }
}

View Class with the Swing elements

import javax.swing.*;

public class View {
    static JFrame credentialManager = new JFrame("Credential Manager");
    static JButton btnCredentialManagerManageUser = new JButton("Manage User");
    static JButton btnCredentialManagerSignOut = new JButton("Sign Out");
    static DefaultListModel<String> listModelCredentials = new DefaultListModel<>();
    static JList<String> listCredentials = new JList(listModelCredentials);
    static JScrollPane scrollPaneCredentials = new JScrollPane(listCredentials);
}

When I execute the main class the list does not appear. I expect the main frame to contain the two button(which appears) and the list with the scrollbar but it does not appear. I have tried many things but any of them work.


Solution

  • A necessary premise, I don't understand the need of absolute positioning elements in Swing instead of using layout managers.

    Said that, you try to position listCredentials, that is inside a scroll pane, instead of the scroll pane itself, so replace:

    View.listCredentials.setBounds(100,100, 75,75);
    

    with

    View.scrollPaneCredentials.setBounds(100,100, 75,75);