簡體   English   中英

從Jlist獲取對象的實例

[英]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);
}

上面的代碼雙擊時會在jList中獲得一個項目,並使用setText和item.toString()設置Jlabel,從而將對象的標簽設置為toString()。

這可行,但是我正在嘗試將java.lang.object轉換為“ Location”類的實例,而不僅僅是類型為object的實例。 因為我無法獲取該類的getName()等中的方法,所以只有toString方法我該做什么,謝謝

嘗試

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

然后您可以調用item.getName()等,

如果不確定對象的運行時類型,可以使用instanceof進行檢查:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM