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?

Edited by Jaydenn: n/a

6 Years
Discussion Span
Last Post by javaAddict

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?

Edited by javaAddict: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.