我有以下问题:我的框架中有 2 个 JList:
listModel = new DefaultListModel();
ownlistModel = new DefaultListModel();
fillList();
ownBookList = new JList(ownlistModel);
ownBookList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ownBookList.setLayoutOrientation(JList.VERTICAL);
ownBookList.setSelectedIndex(-1);
userlist = new JList(listModel);
userlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userlist.setLayoutOrientation(JList.VERTICAL);
userlist.setSelectedIndex(0);
现在,这些是互斥的,因此如果有人单击一个列表中的项目,则应清除另一个列表中的先前选择。
ls2Handler = new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
// System.out.println(ownBookList.getSelectedValue().toString().length());
Global.selectedUser = ownBookList.getSelectedValue().toString();
Global.selectedIndex = ownBookList.getSelectedIndex();
userlist.clearSelection();
updateFields(Global.selectedUser, 1);
}
}
};
lsHandler = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
// System.out.println(userlist.getSelectedValue().toString().length());
Global.selectedUser = userlist.getSelectedValue().toString();
Global.selectedIndex = userlist.getSelectedIndex();
ownBookList.clearSelection();
updateFields(Global.selectedUser, 0);
}
}
};
userlist.addListSelectionListener(lsHandler);
ownBookList.addListSelectionListener(ls2Handler);
在调用值调整函数时,我总是得到 nullpointerException:
在 javax.swing.JList.fireSelectionValueChanged 的 Books$3.valueChanged(Books.java:199) 处的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常(未知来源)
这是为什么?如您所见,它们已被声明并且应该可以访问。