adding elements to a JList


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: adding elements to a JList

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    97

    adding elements to a JList

    ok...im feeling kinda dumb at the moment. in another program i used just a regular List where you could just say something like:

    List.add(someString);

    but now, i decided to use JList in combination with the BreezySwing gui package (mainly for school). so JList does not have the same methods as does List so i was wondering how i would go about adding elements to my list and have them displayed.

  2. #2
    Join Date
    Mar 2004
    Posts
    635

  3. #3
    Join Date
    Dec 2005
    Posts
    97
    yea, i've looked at that but i still cant seem to make it work lol

    Code:
    private JList nameList;
    private DefaultListModel listModel;
    
    // some code
    
    nameList = addList(1,5,1,5); //just putting a blank list in the GUI (shows up at least)
    
    // more code
    
    listModel = new DefaultListModel();
    String name = "";
    
    if(buttonObj == addNameButton) // odd way of checking if the button was clicked but works =P
    {
    	name = nameField.getText(); 
    	listModel.addElement(name);
    	nameList = new JList(listModel); //doesnt add anything to the list			
    }
    so for some reason that doesnt work...havent been able to figure it out yet.

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    because you've created a new list model that isn't associated at all with the list you're using.

    Code:
    nameList = new JList(listModel);
    listModel.addElement(name);
    
    or 
    
    nameList.getModel().addElement(name);

Similar Threads

  1. Replies: 3
    Last Post: 12-03-2005, 05:53 PM
  2. Adding items to a JList at run time.
    By doWhileSomethin in forum Java
    Replies: 2
    Last Post: 11-27-2005, 09:45 PM
  3. Replies: 0
    Last Post: 05-22-2002, 01:04 PM
  4. Adding elements in local XML-File
    By Holger in forum XML
    Replies: 0
    Last Post: 07-23-2001, 02:54 AM
  5. Replies: 1
    Last Post: 07-12-2001, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center