JList disappears when added to JScrollPane

QuantumCore :

I don't know why this is happening, I've looked up nearly every Stack Overflow question regarding this. The error is, When I add a JScrollPane to JList, Nothing shows up. Even if I'm adding it to the frame.

JList mainlist = new JList();
JScrollPane listScroller = new JScrollPane(mainlist);
listScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

mainlist.setToolTipText("List of People");
mainlist.setFont(new Font("Consolas", Font.BOLD, 13));
int pos = mainlist.getModel().getSize();
DefaultListCellRenderer renderer =  (DefaultListCellRenderer)mainlist.getCellRenderer();  
renderer.setHorizontalAlignment(JLabel.CENTER);  
mainlist.setModel(model);
mainlist.setForeground(Color.GREEN);
mainlist.setBackground(new Color(44, 47, 51));
mainlist.setBounds(10, 108, 780, 248);
mainlist.setFixedCellHeight(20);
mainlist.setFixedCellWidth(30);
mainlist.setBorder(new EmptyBorder(10,10, 10, 10)); 
frm.getContentPane().add(listScroller);
R VISHAL :

You should apply bounds to your scrollPane and not Your List

listScroller.setBounds(10, 108, 780, 248);

Also make sure Your Content Pane has null layout for setBounds() to work or better just use an border layout and set the preferred&maximum Size() to what works for you

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=477653&siteId=1