Hi guys.

I am having issues with a program I am writing.

Basically, I have a JList that stores toString() references of "Location" type objects. I want to be able to remove these.

However, the "Location" type objects are stored in an ArrayList, hence requiring an overriding toString() method to display them correctly in the JList.

Now, I want to be able to remove these refernces from the JList AND remove the corresponding entry from my ArrayList. However, the objects stored in the JList are just toString() representations of them. SO my question is, for my method to remove a selection from my JList, how could I tell the method that I want the actual object removed from the ArrayList as well, not just the String format?

To retrieve the current value selected in the JList, the returned value is of type Object:
Object obj = watchList.getSelectedValue();

I have tried casting this back to a Location type object but this doesn't seem to work. Also, knowing that both toString() methods for Locations and Objects would produce a match, I've also tried comparing String representations of both, ie:

while(it1.hasNext()){
            Object obj = watchList.getSelectedValue();
            Location loc = it1.next();
            String locStr = loc.toString();
            if(locStr == obj.toString())
            {watchListCollection.remove(loc)}

But this has no effect either. Hope I've made sense. Can anybody possibly help me here?

Thanks!
Than

Recommended Answers

All 4 Replies

Use a ListModel that takes te ArrayList as a content provider for the JList.
That way, if you remove something from the ArrayList it's automatically removed from the ListModel as well and therefore from the JList's display.

That's the proper way to do things at all times.

Use a ListModel that takes te ArrayList as a content provider for the JList.
That way, if you remove something from the ArrayList it's automatically removed from the ListModel as well and therefore from the JList's display.

That's the proper way to do things at all times.

Thanks for the reply jwenting.

So how would I create a ListModel that uses the ArrayList as a content provider? The ListModel interface cannot be set up to take any parameters.

You can easily extend AbstractListModel to be a tiny wrapper around an ArrayList that's passed to it.

David, I think this example might help. Note that you should still check out the link Ezzaral selected; I only implemented one of three methods that it suggested to implement.

public class Person {
	String ssn = "";
	String firstName = "";
	String lastName = "";
	
	
	public Person(String ssn, String first, String last){
		this.ssn = ssn;
		this.firstName = first;
		this.lastName = last;
	}
}
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JListExample extends AbstractListModel {
	ArrayList<Person> list = new ArrayList<Person>();
	
	@Override
	public Object getElementAt(int arg0) {
		return list.get(arg0).lastName;
	}

	@Override
	public int getSize() {
		return list.size();
	}
	
	public void addPerson(Person p){
		list.add(p);
		fireContentsChanged(this, list.size()-1, list.size()-1);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame jListDisplay = new JFrame();
		JPanel listPanel = new JPanel();
		final JListExample exampleList = new JListExample();
		exampleList.addPerson(new Person("217-25-2525", "Kyle", "InventedName")); //SSN isn't real, !!
		JList list = new JList(exampleList);
		jListDisplay.add(listPanel);
		listPanel.add(list);
		jListDisplay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JButton addPerson = new JButton("Add person");
		final JTextField field = new JTextField("Enter last name");
		addPerson.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String lastName = field.getText();
				exampleList.addPerson(new Person("blankSSN", "Ken", lastName));
			}
			
		});
		
		listPanel.add(field);
		listPanel.add(addPerson);
		jListDisplay.setPreferredSize(new Dimension(300,300));
		jListDisplay.pack();
		jListDisplay.setVisible(true);
	}

}

I hope that helps. The Person class is obviously just a little helper class. Neither class that I posted is completely implemented, but I think it demonstrates the basic concept of how to extend and implement the AbstractListModel class and use it with a JList.

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.