Search code examples
javaswingnetbeansjscrollpanejlist

No Scroller For JList


In my swing application, I set a Vector as data to JList. JList is resizable (vertical and horizontal). Number of viewable items is 8. When data is set morethan 8 items to the JList, the Scroller is not visible and to see the rest of items, JList should be pull down. Is there any way to get this JScroller ?

I use Netbeans IDE 7.2 I checked the JScrollerPane's properties. It has default values for horizontal and vertical scroll bars.

Here is JScrollPane and JList codes which NetBeans has created.
JScrollPane
jScrollPane1 = new javax.swing.JScrollPane();

This is JList

jList1 = new javax.swing.JList();    
jList1.setBackground(new java.awt.Color(##, ##, ##));    
jList1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(##, ##, ##), 1, true));    
jList1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N    
jList1.setForeground(new java.awt.Color(##, ##, ##));      

jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);    
jList1.setCellRenderer(new CellRendererManager());    
jList1.setMaximumSize(new java.awt.Dimension(30, 80));    
jList1.setMinimumSize(new java.awt.Dimension(30, 80));    
jList1.setPreferredSize(new java.awt.Dimension(30, 80));    
jList1.setValueIsAdjusting(true);    
jScrollPane1.setViewportView(jList1);

`


Solution

  • You state:

    JScrollPane lays on FreeDesign layout which shows in NetBeans IDE. JScrollPane has not been set any size.But the JList has set a dimension(30,80)

    There's your problem -- you're setting the Dimension of the JList which prevents it's view from expanding, even if it is held by a JScrollPane.

    Solution: don't do this. Read up on and use the various layout managers so that they and your component's innate preferred sizes do all the sizing and heavy lifting for you.