peytonu5
peytonu5

Reputation: 3

Issue With Java JList

I have tried to use a Jlist but I have not been able to get it to show up in my JFrame.

Here is my code:

private static void list(){
    JFrame frame = new JFrame();
    frame.setTitle("Menu");
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(new FlowLayout());
    listModel = new DefaultListModel();
    listModel.addElement("Add Member");
    listModel.addElement("Add Meeting");
    listModel.addElement("Record Attendance");

    list = new JList(listModel);
    list.setVisibleRowCount(3);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    JScrollPane listScrollPane = new JScrollPane(list);
    frame.add(listScrollPane, BorderLayout.CENTER);
}

Currently when I run the program the frame will open, but it is blank. Any help?

Upvotes: 0

Views: 43

Answers (2)

Mandeep Rajpal
Mandeep Rajpal

Reputation: 1777

Make frame.setVisible(true); the last line of the function.

Upvotes: 1

Clyde D'Cruz
Clyde D'Cruz

Reputation: 2065

You have set the layout as frame.setLayout(new FlowLayout()); but you have used frame.add(listScrollPane, BorderLayout.CENTER); try changing the layout to border layout eg: frame.setLayout(new BorderLayout());

Upvotes: 0

Related Questions