Results 1 to 3 of 3
  1. #1
    _SAS is offline Member
    Join Date
    Jan 2010
    Location
    Russia
    Posts
    9
    Rep Power
    0

    Default 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):
    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);
    Here is what it looks like:

    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your problem is here:

    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);
    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.

  3. #3
    _SAS is offline Member
    Join Date
    Jan 2010
    Location
    Russia
    Posts
    9
    Rep Power
    0

    Default

    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

  1. Displaying panels inside a main panel
    By Sneaky Fox in forum AWT / Swing
    Replies: 4
    Last Post: 01-21-2011, 04:12 PM
  2. JList displaying problems
    By D.Calladine in forum New To Java
    Replies: 0
    Last Post: 12-02-2010, 11:27 PM
  3. Heavyweight Component resizing inside JScrollpane
    By random7 in forum AWT / Swing
    Replies: 2
    Last Post: 07-20-2010, 01:58 PM
  4. Clicking Items inside of a JList
    By hunterbdb in forum AWT / Swing
    Replies: 9
    Last Post: 10-21-2009, 06:50 PM
  5. how to draw an image inside of jscrollpane
    By paty in forum Java Applets
    Replies: 1
    Last Post: 07-24-2007, 12:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •