Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can not clear the contents of JList Java

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Can not clear the contents of JList Java

    Hello friends..I am new to this forum .
    I am developing small application using NetBeans 7.0 which has a JList on the form (from the palette) . I am filling the JList using the vector which contains records from database. But I can not clear the contents of the JList. Please help me friends. Here is my code

     
    private void dateChooserSelectedDatePropertyChange(java.beans.PropertyChangeEvent evt) {                                                       
     
            DefaultListModel model = new DefaultListModel();
            listSerialNo = new JList(model);
     
            UpdatePurchase updatePurchase = new UpdatePurchase();
            Vector<String> serialNo = new Vector<String>();
            serialNo = updatePurchase.getSerialNoForSelectedDate(date);
            if(serialNo.size()>0){
                listSerialNo.setListData(serialNo);
            }
            else{
                model.clear();
                resetFields();
            }
        }

    If I use above code then neither Jlist contains any record nor it clears the contents of JList . If I remove
    DefaultListModel model = new DefaultListModel();
    listSerialNo = new JList(model);
    // if & else code from above code snippet
    model.clear();
    then atleast JList contains some records for specific date if available.

    My aim is if there are records available for specific date then that record should be displayed in JList otherwise JList should be empty. Please guide me.
    Thanks .


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can not clear the contents of JList Java

    Instead of removing invalid data from your List, why not only put valid data in the List in the first place?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can not clear the contents of JList Java

    Quote Originally Posted by Junky View Post
    Instead of removing invalid data from your List, why not only put valid data in the List in the first place?
    @Junky Thanks for reply. In my application,when I select date from JDateChooser if records are present for selected date in database then it will be fetched & displayed in JList. Otherwise error message will be displayed. So even if record are not present for particular date ,records for previous date are in JList. I don't want to display any records in jList in this case.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Can not clear the contents of JList Java

    In that case create a new JList each time you fetch data from the database.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can not clear the contents of JList Java

    Quote Originally Posted by Junky View Post
    In that case create a new JList each time you fetch data from the database.
    Can you tell me , how to create new JList each time. I am not clear with what you are saying. Because I have JList placed on the form from the NetBeans palette & fetched data from the database is displayed in JList.

Similar Threads

  1. How to tranfer contents from Database onto the web using Java
    By abhiM in forum Java Theory & Questions
    Replies: 4
    Last Post: August 5th, 2011, 09:42 AM
  2. an easy clear java programming tutorial
    By zkil_jpf in forum The Cafe
    Replies: 2
    Last Post: April 22nd, 2010, 08:40 AM
  3. How to Use the JList component - Java Swing
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: July 11th, 2009, 04:02 AM
  4. [SOLVED] JAVA JList Problem in visual images
    By antitru5t in forum AWT / Java Swing
    Replies: 4
    Last Post: April 15th, 2009, 03:09 PM
  5. Replies: 1
    Last Post: March 31st, 2009, 02:49 PM

Tags for this Thread