JList and ArrayList update?

If you create your own ListModel you should extend AbstractListModel and when implementing your addElement method, you need to call a fire method (for notifying the user interface for the update), like.

If you create your own ListModel you should extend AbstractListModel and when implementing your addElement method, you need to call a fire-method (for notifying the user interface for the update), like: public void addElement(MyObject obj) { myArrayList. Add(obj); fireIntervalAdded(this, myArrayList.size()-1, myArrayList.size()-1); } You custom ListModel should look something like this: public class MyListModel extends AbstractListModel { private final ArrayList myArrayList = new ArrayList(); public void addElement(MyObject obj) { myArrayList. Add(obj); fireIntervalAdded(this, myArrayList.size()-1, myArrayList.size()-1); } @Override public Object getElementAt(int index) { return myArrayList.

Get(index); } @Override public int getSize() { return myArrayList.size(); } }.

So in the case I need to create the ListModel in my Model class, then I don't need a ArrayList. I could use the ListModel instead no? – wotan2009 Jul 25 at 13:04 @wotan: Your Model class should extend AbstractListModel and you need an ArrayList to have your elements in.It's more work if you want to use ListModel then you need to write the notifying by hand.

– Jonas Jul 25 at 13:05 @wotan: See my update with an example of a custom ListModel. – Jonas Jul 25 at 13:12 Ok I see, thank you. – wotan2009 Jul 25 at 13:13.

You need to use a ListModel to control adding and removing items from a JList. The tutorial is very useful: download.oracle.com/javase/tutorial/uisw... Here is some example code from the tutorial: listModel = new DefaultListModel(); listModel. AddElement("Jane Doe"); listModel.

InsertElementAt(employeeName.getText(), index); int index = list. GetSelectedIndex(); listModel. Remove(index); If you have an arraylist you could build your own List Model around it.

And link for examples java2s. Com/Code/Java/Swing-JFC/List. Htm +1 – mKorbel Jul 25 at 12:24 How can I notify the list model each time I add element to the arraylist?

– wotan2009 Jul 25 at 12:33 Build your own List Model with the ArrayList as the backing. – jzd Jul 25 at 13:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions