开发者

Getting the instance of an object from a Jlist

开发者 https://www.devze.com 2023-02-21 15:53 出处:网络
if(evt.getClickCount() == 2){ int index = locLst.locationToIndex(evt.getPoint()); ListModel dlm = locLst.getModel();
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 instan开发者_JS百科ce 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消