获得所选择的项目。如果我在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