0

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.

  • 1
    Your entire class should be restructured. 1) You should NOT be extending JFrame. 2) You should NOT be using static variables. 3) You should NOT be using a null layout. 4) the GUI SHOULD be created on the EDT. 5) Components should be added to the frame before the frame is made visible. Start by reading the section from the Swing tutoral on [How to Use Lists](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html). Download the "listDemo" example and understand how it works, then make changes for your specific requirements. – camickr Apr 27 '21 at 20:03

2 Answers2

0

the list with the scrollbar but it does not appear

If the scrollpane has a size, it will then display with the JList inside, albeit in the top left corner.

View.scrollPaneCredentials.setSize(new Dimension(300, 300));

To give you an idea of why this occurs, make the following changes:

View.credentialManager.setLayout(new FlowLayout());
//                               ^--- changed from "null"

... and then add these to the bottom of your constructor:

View.credentialManager.pack();
View.credentialManager.setVisible(true);
//                     ^---- move this to bottom 

Notice, the layout manager will display your list, albeit on the far right.

To get a layout manager that's just right, check out this documentation: https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

tresf
  • 7,103
  • 6
  • 40
  • 101
-1

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);
Rocco
  • 1,098
  • 5
  • 11