当尝试通过双击从列表中选择项目时,它会为两个列表选择一次,但是在单击选择之后!!!
listScrollPanel.setViewportView(categoryList);
subCategoryList.setModel(new javax.swing.AbstractListModel() {
String[] strings = {"Sub-category 1", "Sub-category 2", "Sub-category 3", "Sub-category 4", "Sub-category 5", "Sub-category 6"};
});
subCategoryList.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
infoBox(Integer.toString(evt.getClickCount()), subCategory);
if (evt.getClickCount() == 2) {
subCategoryList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
@Override
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
subCategoryListValueChanged(evt);
}
});
}
}
});
private void subCategoryListValueChanged(javax.swing.event.ListSelectionEvent evt) {
subCategory = (String) subCategoryList.getSelectedValue();
labelSC.setText(CAT_HEADER);
listScrollPanel.setViewportView(categoryList);
}
private static void infoBox(String Message, String location) {
JOptionPane.showMessageDialog(null, Message, location, JOptionPane.INFORMATION_MESSAGE);
}
如何让他只听双击? 我有categoryList,双击后ListSelection改变viewportView到subCategory工作相同!但是他们在单击后首先进行2次双击反应!
答案 0 :(得分:5)
每次发生鼠标双击事件时,您都会添加新的ListSelectionListener
。其中每一个都会响应单击鼠标。您可以单独使用MouseListener
:
subCategoryList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 2) {
String selectedValue = subCategoryList.getSelectedValue();
// do stuff with selectedValue...
}
}
});