Results 1 to 6 of 6

Thread: How to write a List Data Listener in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    139

    How to write a List Data Listener in Java?

    Hi friends,
    I have done some basic studies in Java till now. I am trying to write list data listener, so that when the contents of a mutable list change, some event should occur. I know that at such case, list data listener would be useful.!! I have tried lot of different things but was not succeeded. So can anyone please explain me how to write a List Data Listener in Java.?? Thanks in Advance.!!
    The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little.
    -Joe Martin

  2. #2
    Join Date
    Aug 2006
    Posts
    235

    Re: How to write a List Data Listener in Java?

    I think that in such case that you have mentioned only a list data listener would be used. Since you don't know much about the list data listener, I thought that I should provide some basic information about the same. List data events occur when the contents of a mutable list change. The first thing that you will have to do is to register a list data listener with the list model because the model fires these events. You will have to keep in mind that components doesn't fires these events. If you have not explicitly created a list with a mutable list model, then your list is immutable, and its model will not fire these events.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to write a List Data Listener in Java?

    You should also know about the methods that are used by the list data listener, before using it in coding. There is no corresponding adapter class for ListDataListener. The following are the methods of ListDataListener Interface :
    • intervalAdded(ListDataEvent) - This method is called when one or more items have been added to the list.
    • intervalRemoved(ListDataEvent) - When one or more items have been removed from the list, this method is called.
    • contentsChanged(ListDataEvent) - This method is called when the contents of one or more items in the list have changed.

  4. #4
    Join Date
    Mar 2008
    Posts
    672

    Re: How to write a List Data Listener in Java?

    You should also know the classes that are used by the list data listener, because it is very important from the coding point of view. Once you know what exactly to do, then you can use the approximate class. The following are the classes of ListDataEvent API :
    • Object getSource() - This method returns the object that fired the event.
    • int getType() - The event type is returned when this method is called.
    • int getIndex0() - This method returns the index of the first item whose value has changed.
    • int getIndex1() - This method returns the index of the last item whose value has changed.

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

    Re: How to write a List Data Listener in Java?

    You will always have to register a list data listener. The following code explains how to registers a list data listener on the list model and implements the listener :
    Code:
    private DefaultListModel listModel;
    ...
    listModel = new DefaultListModel();
    ...
    listModel.addListDataListener(new MyListDataListener());
    
    class MyListDataListener implements ListDataListener {
        public void contentsNew(ListDataEvent le) {
            log.append("contentsNew: " + le.getIndex0() +
    	           ", " + le.getIndex1() + newline);
        }
        public void intervalAdded(ListDataEvent le) {
            log.append("intervalAdded: " + le.getIndex0() +
    	           ", " + le.getIndex1() + newline);
        }
        public void intervalRemoved(ListDataEvent le) {
            log.append("intervalRemoved: " + le.getIndex0() +
    	           ", " + le.getIndex1() + newline);
        }
    }

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

    Re: How to write a List Data Listener in Java?

    If you want to return an ImageIcon then following coding would help you. This coding also returns null if the path was invalid. Here is the sample of the code :
    Code:
    protected static ImageIcon createImageIcon(String imageName) {
            String imgLocation = "toolbarButtonGraphics/navigation/"
                                 + imageName
                                 + ".gif";
            java.net.URL imageURL = ListDataEventTrial.class.getResource(imgLocation);
    
            if (imageURL == null) {
                System.err.println("Resource was not found: "
                                   + imgLocation);
                return null;
            } else {
                return new ImageIcon(imageURL);
            }
        }

Similar Threads

  1. How to write a Document Listener in Java?
    By pushpendra in forum Software Development
    Replies: 8
    Last Post: 18-03-2010, 01:22 PM
  2. How to write a Key Listener in Java?
    By N I C K in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:52 AM
  3. How to write a Focus Listener in Java?
    By The Recruiter in forum Software Development
    Replies: 4
    Last Post: 12-02-2010, 07:05 AM
  4. How to write a Change Listener in Java?
    By Samarth in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 06:10 AM
  5. How to write an Action Listener in Java?
    By Beter 2 Burn Out in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 05:44 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,751,640,192.48534 seconds with 16 queries