My entire project is fucked if I can't do this;

Basically, I need to be able to add an element to a JList (doesn't need to be a JList, just needs to be a list component) then have a tag added to the element too, so when I get the selected list item I can cast it to the object I need... I can't really explain it in words, let me explain it in code.

public class MyObject {
	public int i;
	public MyObject(int i) {
		 this.i = i;
	}
}

public class Main {
	public void addJListElement() {
		jList.addElement("My object", new MyObject(10));
	}
	
	public void getSelectedListElement() {
		MyObject myObject = (MyObject)jList.getSelectedElement().getTag();
		int i = myObject.i; //should be 10.
	}
}

Please excuse the poor code, I was rushing to create an example.

Is there anyway I can make this possible, or do something similar?

Doesn't the JList already do what you want? Why do you need the tag?

MyObject myObject = (MyObject)jList.getSelectedElement();

Have you looked the API for JList, or examples?

EDIT. Why don't you put "My Object" as a String attribute in the class MyObject and loop the JList and search by that?

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.