Results 1 to 3 of 3
  1. #1
    mrbeast87 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default JList doesn't show the elements

    Hi everyone,

    I'm facing a problem here. I have a dynamic JList (with DefaultListModel) in a JScrollPane. This is the code I'm using to initialize the components:

    Java Code:
            listPanel = new JPanel(new BorderLayout());
            listPanel.setName("listPanel");
    
            listModel = new DefaultListModel();
            
            list = new JList(listModel);
            list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
            list.setLayoutOrientation(JList.VERTICAL);
            
            JScrollPane listScroller = new JScrollPane(list);
    		
            listPanel.add(listScroller, BorderLayout.CENTER);

    As you may have noticed, the list starts empty. After some user interaction, this method is called:

    Java Code:
            public void populateList(List<String> elements) {
    		listModel.clear();
    		for(String s : elements) {
    			listModel.addElement(s);			
    		}
    	}
    Sometimes this works just fine. But once in a while, after the populateList method is called, the list becomes empty! I double checked it, and the argument List<String> elements isn't empty. It just seems like a refresh problem of some kind... Do you know why this happens and how can I solve it?

    Any help that you could provide is welcome! :)
    Cheers

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    10

    Default

    Sometimes this works just fine.
    When you get random results then you need to make sure the code is executed on the EDT.

    See: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  3. #3
    mrbeast87 is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Wow, thanks camickr! Now I know I needed to invoke the Swing thread :) it's working like a charm

Similar Threads

  1. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  2. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:24 PM
  3. Replies: 3
    Last Post: 05-27-2010, 10:54 AM
  4. How could I search elements in a Jlist?Please Help!
    By Pro Alinio in forum New To Java
    Replies: 4
    Last Post: 03-25-2009, 11:49 PM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 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
  •