Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    44
    Thanks
    5
    Thanked 0 Times in 0 Posts

    repopulating JLists?

    Hello I am making a program that auto clears and then repopulates a JList depending on what you have done.

    I have managed to clear all the entries in the JList but can't seem to add any
    Code:
    private static ConstantsS[] = {};
    private static VariablesS[] = {};
    private void VarConUpdate()
    	{
    		ConstantsList.removeAll();
    		VariablesList.removeAll();
                    //what i want to do is put all of the VariableS[] in VariablesList
                    //and all ConstantsS[] ConstantsList
                    //how would I achieve this?
    	}
    public GUIOutput()
    {
                   JScrollPane ScrollPaneConstants = new JScrollPane();
    		ScrollPaneConstants.setBounds(636, 30, 126, 320);
    		getContentPane().add(ScrollPaneConstants);
    
    		ConstantsList = new JList();
    		ScrollPaneConstants.setViewportView(ConstantsList);
    		ConstantsList.setVisibleRowCount(5);
    }

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,887
    Thanks
    43
    Thanked 202 Times in 201 Posts
    Code:
    foreach(String s in VariableS)
    {
      VariablesList.Add(s);
    }
    Should be something very close to that... could also do a for loop...
    Code:
    for(int i = 0; i < VariableS.Length; i++)
    {
      VariablesList.Add(VariableS[i]);
    }

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    44
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thanks what i did in the first place worked but it was due to another problem in my program not my loop.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Its not quite as easy in Java as it is in C# to add to a jlist. If you want to actually add an item to a JList, you need to add it to the list's model. That's a simple matter of retrieving the list model, and casting it:
    PHP Code:
    ((DefaultListModel)mylist.getModel()).addElement(anObject); 
    Should do it.


  •  

    Posting Permissions

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