Results 1 to 3 of 3
- 07-31-2011, 07:50 PM #1
Member
- Join Date
- Jan 2010
- Location
- Russia
- Posts
- 9
- Rep Power
- 0
JList inside JScrollPane is not displaying
Hello. I've got a problem with a JList. I create a JScrollPane and place my JList inside it, then set the preferred size of the JScrollPane. When the GUI is shown, my JScrollPane is visible and has the correct size, however, the JList appears to have zero width and height (according to the debugger). When I add elements to JList's model they are added correctly, but the list is still invisible.
Here's some code.
In the panel constructor (it has a BorderLayout, and the JScrollPane with my list is added to the left side):
Here is what it looks like:Java Code:pictures = new DefaultListModel(); pictures.addElement("test"); picList = new JList(pictures); picList.setLayoutOrientation(JList.VERTICAL); picList.setVisibleRowCount(20); picList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane pPic = new JScrollPane(picList); pPic.setPreferredSize(new Dimension(200, 0)); pPic.add(picList); add(pPic, BorderLayout.WEST);

As you can see, there's an empty space on the left side, where my list is supposed to be. Thank you very much for any help.
-
Your problem is here:
This replaces the JScrollPane's view port which is not what you want to do. Rather just add the list to the JScrollPane in its constructor as you do 2 lines above, either that or add it to the scrollpane's viewport. Also your dimension of 200, 0 seems odd.Java Code:pictures = new DefaultListModel(); pictures.addElement("test"); picList = new JList(pictures); picList.setLayoutOrientation(JList.VERTICAL); picList.setVisibleRowCount(20); picList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane pPic = new JScrollPane(picList); pPic.setPreferredSize(new Dimension(200, 0)); [color="red"][b]pPic.add(picList); // ***** here *****[/b][/color] add(pPic, BorderLayout.WEST);
- 07-31-2011, 08:45 PM #3
Member
- Join Date
- Jan 2010
- Location
- Russia
- Posts
- 9
- Rep Power
- 0
Thank you, it worked!
This line is here because previously pPic was a JPanel, and I just didn't notice it when I changed the code.
As for preferred size, I need the fixed width of the list (200), and the preferred height is ignored when I add the JScrollPane to BorderLayout.WEST. It is stretched vertically.
Now everything works fine.
Similar Threads
-
Displaying panels inside a main panel
By Sneaky Fox in forum AWT / SwingReplies: 4Last Post: 01-21-2011, 04:12 PM -
JList displaying problems
By D.Calladine in forum New To JavaReplies: 0Last Post: 12-02-2010, 11:27 PM -
Heavyweight Component resizing inside JScrollpane
By random7 in forum AWT / SwingReplies: 2Last Post: 07-20-2010, 01:58 PM -
Clicking Items inside of a JList
By hunterbdb in forum AWT / SwingReplies: 9Last Post: 10-21-2009, 06:50 PM -
how to draw an image inside of jscrollpane
By paty in forum Java AppletsReplies: 1Last Post: 07-24-2007, 12:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks