将 ListModels 设置为 JList
我有一段代码,它根据文件的内容设置 ListModel 并将其添加到 jList 中。当我第一次选择文件时,代码运行正确。当我选择第二个文件时,它给出数组越界异常。 runList.setModel(model) 发生错误;
这是代码
protected class OpenFileListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae) {
try
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("*.txt", "txt"));
fileChooser.setCurrentDirectory(new File("."));
int result = fileChooser.showOpenDialog(null);
fileName = fileChooser.getSelectedFile().getPath();
molfindOutText.setText(getFileName());
DefaultListModel model = new DefaultListModel();
runList.setModel(model);
File f = new File(getFileName());
Scanner scanner = new Scanner(f);
lineNo = 0 ;
int i = 0;
idLines = new ArrayList();
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
lineNo = lineNo+1;
if (line.matches(".*COMPOUND_IDENTIFICATION_RUN.*"))
{
model.add(i, line);
runList.setSelectedIndex(0);
idLines.add(lineNo);
i++;
}
}
currentRun = 1;
if (idLines.size()>1)
{
extractRunInfo(idLines.get(0),idLines.get(1)-2);
}
else
{
extractRunInfo(idLines.get(0),lineNo);
}
scanner.close();
runList.addListSelectionListener(lsl);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
PrintStackTrace
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(ArrayList.java:324)
at edu.uconn.pharmacy.molfind.AnalysisPanel$1.valueChanged(AnalysisPanel.java:99)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1795)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1809)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:559)
at javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:403)
at javax.swing.JList.clearSelection(JList.java:2043)
at javax.swing.JList.setModel(JList.java:1676)
at edu.uconn.pharmacy.molfind.AnalysisPanel$OpenFileListener.actionPerformed(AnalysisPanel.java:131)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6373)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6138)
at java.awt.Container.processEvent(Container.java:2085)
at java.awt.Component.dispatchEventImpl(Component.java:4735)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
at java.awt.Container.dispatchEventImpl(Container.java:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:652)
at java.awt.EventQueue$2.run(EventQueue.java:650)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I have a piece of code that setup a ListModel based on content of a file and add it to a jList. The code runs correctly when I select a file for the first time. When I select a second file it gives Array Out of Bounds Exception. Error occurs at runList.setModel(model);
Here is the code
protected class OpenFileListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae) {
try
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("*.txt", "txt"));
fileChooser.setCurrentDirectory(new File("."));
int result = fileChooser.showOpenDialog(null);
fileName = fileChooser.getSelectedFile().getPath();
molfindOutText.setText(getFileName());
DefaultListModel model = new DefaultListModel();
runList.setModel(model);
File f = new File(getFileName());
Scanner scanner = new Scanner(f);
lineNo = 0 ;
int i = 0;
idLines = new ArrayList();
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
lineNo = lineNo+1;
if (line.matches(".*COMPOUND_IDENTIFICATION_RUN.*"))
{
model.add(i, line);
runList.setSelectedIndex(0);
idLines.add(lineNo);
i++;
}
}
currentRun = 1;
if (idLines.size()>1)
{
extractRunInfo(idLines.get(0),idLines.get(1)-2);
}
else
{
extractRunInfo(idLines.get(0),lineNo);
}
scanner.close();
runList.addListSelectionListener(lsl);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
PrintStackTrace
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(ArrayList.java:324)
at edu.uconn.pharmacy.molfind.AnalysisPanel$1.valueChanged(AnalysisPanel.java:99)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1795)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1809)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:559)
at javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:403)
at javax.swing.JList.clearSelection(JList.java:2043)
at javax.swing.JList.setModel(JList.java:1676)
at edu.uconn.pharmacy.molfind.AnalysisPanel$OpenFileListener.actionPerformed(AnalysisPanel.java:131)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6373)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6138)
at java.awt.Container.processEvent(Container.java:2085)
at java.awt.Component.dispatchEventImpl(Component.java:4735)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
at java.awt.Container.dispatchEventImpl(Container.java:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:652)
at java.awt.EventQueue$2.run(EventQueue.java:650)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误似乎出现在您在列表上注册的选择侦听器中,该侦听器正在尝试访问(现在为空)列表的元素。堆栈跟踪中的第二个条目告诉您在哪里查找:
可能您只需要 valueChanged() 来检查列表是否为空,并且在这种情况下不执行任何操作。
It would appear that the error is in a selection listener you have registered on the list, which is trying to access an element of the (now empty) list. The second entry in the stack trace tells you where to look:
Probably you just need valueChanged() to check if the list is empty and do nothing in that case.