Results 1 to 5 of 5

Thread: Confused with DefaultListModel class

  1. #1
    Join Date
    Dec 2009
    Posts
    38

    Confused with DefaultListModel class

    Hello friend,

    I need you help to to solve one of my confusion about the classes of java programming. I am totally confused about the 'DefaultListModel class' of advanced java programming. I want to be aware about the functionality of 'DefaultListModel class' of java. If you have sound knowledge about 'DefaultListModel class' of java, then please try to share it with me. Your any help would be greatly appreciated.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Confused with DefaultListModel class

    Hi,

    This class loosely implements the java.util.Vector API, in that it implements the 1.1.x version of java.util.Vector, has no collection class support, and notifies the ListDataListeners when changes occur. Presently it delegates to a Vector, in a future release it will be a real Collection implementation. The DefaultListModel class implements two interfaces basically known as ListModel and Serializable.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Confused with DefaultListModel class

    Please review following example of DefaultListModel class of java. In which I have used it's various methods:
    Code:
    import javax.swing.DefaultListModel;
    import javax.swing.JList;
    
    public class DefaultListModelMain 
    {
      public static void main(String[] argvo) throws Exception {
        DefaultListModel model1 = new DefaultListModel();
        JList list1 = new JList(model1);
    
        int pos1 = 0;
        model1.remove(pos1);
    
        pos1 = model1.getSize() - 1;
        if (pos1 >= 0) {
          model1.remove(pos1);
        }
       
        model1.clear();
      }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Confused with DefaultListModel class

    Hi friend,

    I recommend you to study following methods of DefaultListModel class:
    • setElementAt(Object objc, int idx)
    • set(int index, Object element)
    • removeRange(int fromIdx, int toIdx)
    • removeElementAt(int idx)
    • removeElement(Object objc)
    • lastIndexOf(Object elem1, int idx)

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Confused with DefaultListModel class

    Hi,

    If you want to add item to the object list using the addElement(Object obj) method of DefaultListModel class, then please review following example:
    Code:
    import javax.swing.DefaultListModel;
    import javax.swing.JScrollPane;
    
    public class DefaultListModelDM {
    
      public static void main(final String argsw[]) {
        String labels1[] = { "A", "B", "C", "D", "E" };
        JFrame FB = new JFrame("Sizing Samples");
        FB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        DefaultListModel model = new DefaultListModel();
        model.ensureCapacity(1000);
        for (int ir = 0; ir < 100; ir++) {
          for (int jr = 0; jr < 5; jr++) {
            model.addElement(labels[jr]);
          }
        }
        JList jlist2 = new JList(model);
        jlist2.setVisibleRowCount(4);
        jlist2.setFixedCellHeight(12);
        jlist2.setFixedCellWidth(300);
        JScrollPane scrollPane2 = new JScrollPane(jlist2);
        FB.add(scrollPane2, BorderLayout.CENTER);
    
        FB.setSize(400, 350);
        FB.setVisible(true);
      }
    }

Similar Threads

  1. Confused about Scrollbar class
    By Jagruti23 in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:18 PM
  2. Confused about the List class
    By Jagruti23 in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 04:17 PM
  3. Confused with JSpinner class
    By Bottlenecked in forum Software Development
    Replies: 4
    Last Post: 15-02-2010, 09:51 AM
  4. Confused with OverlayLayout class
    By Bottlenecked in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 09:37 AM
  5. Confused with 'Class SpinnerNumberModel'
    By Khan Baba in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 11:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,252,408.85171 seconds with 17 queries