Hi!

Is it possible to dynamically update a text of items in JList? Let's say there is JList defined as follows:

list1.setModel(new AbstractListModel() {
	Object[] values = listOfTasks.toArray();
	public int getSize() { return values.length; }
	public Object getElementAt(int i) { return "Task " + i; }	
});

As a result, let's say JList includes as following data:
X1
X2
X2

After pressing on JButton, an item in JList must be renamed to "Y". The problem is that I cannot find a procedure like "setElementAt" or something similar. So, how could I solve this problem?

P.S.: I tried to include the following procedure to AbstractListModel, but it didn't help:
public void setValueAt(int i) { values = "Y";

Your setValue looks OK as far as it goes, but there's no way for the JList to know that you've done that, so it doesn't update itself.
After changing your model's data you must call fireContentsChanged (inherited from AbstractListModel) to notify listeners (including the JList) that something has changed - something like fireContentsChanged(this, i, i); ... but you should check out the API doc yourself, of course.

Thank you. But my problem is that I cannot access "setElementAt" when using "list1.getModel().setElementAt(..);" This is my current code:

list1.setModel(new DefaultListModel() {
				Object[] values = listOfTasks.toArray();
				@Override
				public int getSize() { return values.length; }
				@Override
				public Object getElementAt(int i) { return "Task " + i; }
				@Override
				public void setElementAt(Object obj, int i) { values[i] = obj; fireContentsChanged(this, i, i); }	
			});

...

// THIS LINE IS INCORRECT AND I DON'T UNDERSTAND WHY. THE METHOD setElementAt IS 
// SIMLY INACCESSIBLE
list1.getModel().setElementAt("Task is opened", list1.getSelectedIndex());

So, how could I execute "setElementAt" method?

Ok, I've made it working in the following way:

((DefaultListModel)list1.getModel()).setElementAt(...)

Now I don't have a compilation error, but my JList is not updated. Why?

Did you try what I said in my previous post?

After changing your model's data you must call fireContentsChanged (inherited from AbstractListModel) to notify listeners (including the JList) that something has changed - something like
fireContentsChanged(this, i, i);
... but you should check out the API doc yourself, of course.

yes, of course I included "fireContentsChanged(this, i, i);". Look at the line 8. But it doesn't work... Hmm

Sorry - missed that when scanning your code.

Maybe, you could suggest me something else?

If this were my problem I'd be forced back to brute force debugging at this stage. I'd start by adding a few prints to confirm that the model data has actually changed, and that getElementAt returns the new value.
I's also write a tiny ListDataListener and check that the update is triggering the notifications we expect.
Sorry if that's not very helpful, but when all the code looks OK there's no real substitute for step-by-step "what was it I misunderstood" brute-force checking.
Good luck!
Or maybe someone else has some better insight? Come on guys!

Wait a minute..

public Object getElementAt(int i) { return "Task " + i; }

you can call setElementAt from here to next Tuesday, but getElementAt will always return the same value!

Yeeees, a lot of thanks!!!

:)
Mark as solved?

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.