简体   繁体   中英

Getting the instance of an object from a Jlist

if(evt.getClickCount() == 2){
int index = locLst.locationToIndex(evt.getPoint());
ListModel dlm = locLst.getModel();
Object item = dlm.getElementAt(index);;
locLst.ensureIndexIsVisible(index);
System.out.println("Double clicked on " + item);
//Location loct = item.getClass();
DeedCard.setVisible(true);
TitleDeedLbl.setText(item.toString());
System.out.println(item.getClass);
item.equals(loc);
System.out.println(loc);
System.out.println(ha);
}

The above code gets an item in a jList when you double click on it, and sets a Jlabel with setText and item.toString() so it sets the label of the object toString().

this works, but i'm trying to convert a java.lang.object to an instance of the class "Location" class, not of just type object. as i can't get the methods that are in that class getName() etc, only the toString method what do i do, thanks

Try

Location item = (Location) dlm.getElementAt(index);

and then you can call item.getName() etc.,

If you aren't sure about the runtime type of an object you can check it with instanceof :

Object obj = dlm.getElementAt(index);
if (obj instanceof Location){
 Location item = (Location) obj;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM