如果我在 ComboBox 中有三个项目:
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
}
});
非常不愉快的问题。我仅在单击组合框(弹出窗口)时才使用组合框,因此我仅在此时添加其项目,然后将其删除。尽管如此,第一个/标题项目永远不会更新或者它只是空白,我无法使用字段保存最后一个选择状态,现在我也无法通过 ArrayList 的帮助来完成。我尝试了鼠标和项目侦听器,但每次都失败了,因为它总是返回第一个元素。
您是否知道如何跟踪所选内容,即使仅在弹出窗口中处理它?我非常感谢你!