Hi everybody,

I have a class called "dynamicArray" which makes use of static array and provides addElement, deleteElement methods, etc. It stores the datatype, "myObject". I want to construct the JList with the instance of dynamicArray class, say myDynamicObj. so that whenever when I delete/add element from/to dynamicArray the JList will automatically updated with the help of the fireContentsChanged method. But, I could not find a way to construct the JList with myDynamicObj. Please help...

Note that dynamicArray class extends the AbstractListModel.

Thanks..

Recommended Answers

All 10 Replies

Here is the some part of the code:

myDynamicArray myArray = new myDynamicArray();
AbstractListModel model = myArray ;
JList lstImage = new JList(model);

when clicked on the button I add some object to myArray:
myArray.addElement(newImage);

but JList display nothing. What can be the reason?

If your class implements the ListModel interface (which it does if you extend AbstractListModel), why not use the constructor JList(ListModel dataModel)?
Edit: Nevermind, you posted the code while I was composing this.

Could you give a bit more detail? What is the dataModel here? I have to construct the JList with dynamicArray object.

Do you mean this:
myDynamicArray myArray = new myDynamicArray();
ListModel model = myArray ;
JList lstImage = new JList(model );

but it didnot work.

Should I write the code for the fireContentsChanged method in the myDynamicArray class? When I add/romove any item I just call: fireContentsChanged(this, 0, ind);

I added this lines to the add method:
fireIntervalAdded( this, 0, ind );
this.fireContentsChanged(this, 0, ind);

but JList still display nothing...

is there anything wrong with the Jlist Construction:
myDynamicArray myArray = new myDynamicArray();
ListModel model = myArray ;
JList lstImage = new JList(model);

You may need to change the interval indexes in fireIntervalAdded() to reflect the new interval.

Your construction is fine. You could even just use JList lstImage = new JList(myArray); without the intermediate variable 'model'.

I have no much experience in Java, and especially in ListModels. So, I have many questions in my mind,
and if you would be more informative and clear in your answers, I will get the point more easily.

Firstly, I have this method in myDynamicArray class:

@Override
    public void fireContentsChanged(Object data, int  first_index, int last_index){

    }

As you can see, there is no implementaion. Is this correct?

Secondly, this my constructor for the Jlist:

myDynamicArray myArray = new myDynamicArray();

ListModel model = myArray ;
    JList lstImage = new JList(model);

Is it defined correctly?

Thirdly, here is the addMethod of myDynamicArray and this method is called when adding new object to myArray:

public void addElement(myObject data){
        // The size is sufficient
         if ( ind < length ) {
             arr[ind] = data;
             ind++;     
             fireIntervalAdded(this, 0, ind);
             this.fireContentsChanged(this, 0, ind);
         } else {
             // Increase the size of the array
             myObject [] newArr = new myObject [length + 5];
             for ( int i=0;i<length;i++){
                 newArr[i] = arr[i];
             }             
             ind = length;
             length += 5;             
             arr = newArr; 

             arr[ind] = data;
             ind++;
             fireIntervalAdded(this, 0, ind);
             this.fireContentsChanged(this, 0, ind);
         }
     }

Is there anything wrong with the fireIntervalAdded, and fireContentsChanged methods?

I have no much experience in Java, and especially in ListModels. So, I have many questions in my mind,
and if you would be more informative and clear in your answers, I will get the point more easily.

Firstly, I have this method in myDynamicArray class:

@Override
    public void fireContentsChanged(Object data, int  first_index, int last_index){

    }

As you can see, there is no implementaion. Is this correct?

You do not need to override that method. It is there in AbstractListModel to handle notifying the JList of the change to the model. By overriding it, you are actually removing that functionality.

Secondly, this my constructor for the Jlist:

myDynamicArray myArray = new myDynamicArray();

ListModel model = myArray ;
    JList lstImage = new JList(model);

Is it defined correctly?

It's fine, though you could just use JList lstImage = new JList(myArray); . You don't need the ListModel model = myArray ; part because myDynamicArray extends AbstractListModel, which implements ListModel.

Thirdly, here is the addMethod of myDynamicArray and this method is called when adding new object to myArray:

public void addElement(myObject data){
        // The size is sufficient
         if ( ind < length ) {
             arr[ind] = data;
             ind++;     
             fireIntervalAdded(this, 0, ind);
             this.fireContentsChanged(this, 0, ind);
         } else {
             // Increase the size of the array
             myObject [] newArr = new myObject [length + 5];
             for ( int i=0;i<length;i++){
                 newArr[i] = arr[i];
             }             
             ind = length;
             length += 5;             
             arr = newArr; 

             arr[ind] = data;
             ind++;
             fireIntervalAdded(this, 0, ind);
             this.fireContentsChanged(this, 0, ind);
         }
     }

Is there anything wrong with the fireIntervalAdded, and fireContentsChanged methods?

Try this instead

public void addElement(myObject data){
        // The size is sufficient
         if ( ind < length ) {
             arr[ind] = data;
             fireIntervalAdded(this, ind, ind);
             ind++;
         } else {
             // Increase the size of the array
             myObject [] newArr = new myObject [length + 5];
             for ( int i=0;i<length;i++){
                 newArr[i] = arr[i];
             }             
             ind = length;
             length += 5;             
             arr = newArr; 

             arr[ind] = data;
             fireIntervalAdded(this, ind, ind);
             ind++;
         }
     }

I did what you exactly said but JList still display nothing when I add new element to myDynamicArray:
myImage newImage = new myImage(file);
myArray.addElement(newImage);

Is it because newImage is a type of a class and JList could not display it properly?

Thank you too much.

That may be the case. It would depend on what "myImage" is. The default cell renderer may not be able to deal with it. Have you tried adding a few strings to the model and see if that works properly?

If you are wanting to put images in a JList, this article might help: http://www.rgagnon.com/javadetails/java-0203.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.