獲得所選擇的項目。如果我在ComboBox三個項目:爪哇 - 這是不可能從JComboBox中
123,456和789,這是唯一可能獲得的第一個(123),忽略所有的休息。
mainCombo.addPopupMenuListener(new PopupMenuListener() {
ArrayList<Object> selectionSaver = new ArrayList<Object>();
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
if (mainList.getSelectedValue() != null) {
ArrayList<Object> arrayValue = mainMethods.returnArrayList(mainList.getSelectedValue());
for (int i = 0; i < arrayValue.size(); i++) {
mainCombo.addItem(arrayValue.get(i));
}
Object lastSelected = mainCombo.getSelectedItem(); // It gets the bloody first and never the other ones, even when I select them.
selectionSaver.add(lastSelected); // It adds the bloody first that was captured.
System.out.println(selectionSaver); // Prints only the first, because it was selected by default.
}
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
mainCombo.removeAllItems();
}
@Override
public void popupMenuCanceled(PopupMenuEvent arg0) {
// TODO Auto-generated method stub
}
});
非常不愉快的問題。我只在單擊它時彈出組合框(Popup),因此我在此時添加其項目,然後刪除它們。儘管如此,第一個/標題Item永遠不會更新或只是空白,我不能保存最後一個選擇狀態的字段,現在我不能通過ArrayList的幫助。我嘗試過鼠標和項目監聽器,每次都失敗,因爲它總是返回第一個元素。
對於如何保持所選內容的軌跡,即使僅在彈出框內處理它,您是否有任何想法?我非常感謝你!
考慮創建併發布的[SSCCE(http://sscc.org)。 – 2013-05-05 19:48:49
並沒有理由PopupMenuListener,添加ListSelectionListner派生JList,我的故事結束:-) – mKorbel 2013-05-05 20:29:56
哇,這真的是一個很好的主意..我沒有想到這一點:) ..我會盡力解決它與這個。 – 2013-05-05 20:37:07