Reputation: 3
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
Reputation: 1777
Make frame.setVisible(true);
the last line of the function.
Upvotes: 1
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