我已經創建了下列實現ListSelectionListener接口的類。這個類應該「監聽」我創建的JList的選擇事件。每次使用單擊該列表的一行時,selected_row值應該更新,因此應該更改字符串「所選的格式行是......」。但是,在多次單擊行之後,select_row值不會更改。任何人都可以爲我提供一個解釋,希望能有辦法做到我想要的嗎?提前致謝!!valueChanged in ListSelectionListener not working
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import ee.dobax.portal.CommonPath;
public class FormatListSelectionListener implements ListSelectionListener{
public ContentGenerated content;
private CommonPathList path_list;
private ConfigRenderingDialog dialog;
public FormatListSelectionListener(ConfigRenderingDialog dialog){
content = dialog.content;
path_list = dialog.pathList;
}
public void valueChanged(ListSelectionEvent e) {
int selected_row;
if(e.getValueIsAdjusting() == false){
selected_row = e.getLastIndex();
System.out.println("The format row selected is "+selected_row);
path_list.addFormatListRowSelected(selected_row);
List<CommonPath> list_p = content.getPathList(selected_row);
Object[] path_list_to_array = new Object[list_p.size()];
path_list.getContents().removeAllElements();
for(int x = 0; x < list_p.size(); x++){
path_list_to_array[x] = list_p.get(x);
path_list.getContents().addElement(path_list_to_array[x]);
}
}
}
}
((JList的)e.getSource())getSelectedIndex()不檢索任何東西.... – Anto 2010-09-28 13:48:38
@Tony:你確定的東西其實是被選中的呢? – 2010-09-28 14:33:16
是的,在FormatList構造函數類中我有 this.setSelectedIndex(0); – Anto 2010-09-28 15:55:18