내 RemoveListener 클래스에서 런타임 오류가 발생하는 이유는 무엇입니까? 내 ChannelListener 클래스가 포함되어 있으면 어떻게됩니까? 내가 내 ChannelListener 클래스를 주석 처리하면 RemoveAction 클래스가 작동합니다. 따라서 을 기대할 수 있습니다. 두 클래스를 함께 사용하려면 어떻게해야합니까?GUI actionPerformed valueChanged problems
예외 글 "AWT-EventQueue의-0" java.lang.ArrayIndexOutOfBoundsException : -1 java.util.ArrayList.get (ArrayList.java:323)에서 GuiDriver $ ChannelListListener.valueChanged에서 (GuiDriver 된 .java 121) javax.swing.DefaultListSelectionModel.fireValueChanged에서 javax.swing.DefaultListSelectionModel.fireValueChanged (DefaultListSelectionModel.java:187) javax.swing.DefaultListSelectionModel.fireValueChanged (DefaultListSelectionModel.java:167에서 ) (로 DefaultListSelectionModel에서 .java : 214) at javax.swing.Defa ultListSelectionModel.removeIndexInterval javax.swing.AbstractListModel.fireIntervalRemoved에서 javax.swing.plaf.basic.BasicListUI $ Handler.intervalRemoved (BasicListUI.java:2204) 에서 (DefaultListSelectionModel.java:611) (AbstractListModel.java:161) GuiDriver $ RemoveAction.actionPerformed에서 javax.swing.DefaultListModel.removeElement (DefaultListModel.java:364) (GuiDriver.java:98)에서
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
public class GuiDriver extends JFrame{
JList channelTitleJList, itemTitleJList;
DefaultListModel cModel, iModel;
List<RssReader> feedList = new ArrayList<RssReader>();
int nextFeed=0;
ListSelectionModel lsm;
public void addFeed(RssReader feed){
feedList.add(feed);
}
public JToolBar createToolBar(){
JToolBar bar = new JToolBar();
Action newToolBarButton = new AddAction("New");
Action deleteToolBarButton = new RemoveAction("Delete");
Action clearToolBarButton = new ClearAction("Clear");
bar.add(newToolBarButton);
bar.add(deleteToolBarButton);
bar.add(clearToolBarButton);
bar.setFloatable(false);
return bar;
}
public JSplitPane createJSplitPane(){
JSplitPane hSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,createChannelJScrollPane(), createItemJScrollPane());
hSplitPane.setDividerLocation(150);
return hSplitPane;
}
public JScrollPane createChannelJScrollPane(){
cModel = new DefaultListModel();
channelTitleJList = new JList(cModel);
JScrollPane channelJScrollPane = new JScrollPane(channelTitleJList);
channelTitleJList.setVisibleRowCount(20);
lsm = channelTitleJList.getSelectionModel();
lsm.addListSelectionListener(new ChannelListListener());
return channelJScrollPane;
}
public JScrollPane createItemJScrollPane(){
iModel = new DefaultListModel();
itemTitleJList = new JList(iModel);
JScrollPane itemJScrollPane = new JScrollPane(itemTitleJList);
//itemTitleJList.addListSelectionListener(new ItemListListener());
return itemJScrollPane;
}
public class AddAction extends AbstractAction{
public AddAction(String name){
super(name);
}
public void actionPerformed(ActionEvent e){
System.out.println(getValue(Action.NAME)+" selected.");
JOptionPane message = new JOptionPane();
String firstInput = message.showInputDialog(null, "Enter URL");
try{
DumpStockPage.readXml(firstInput);
File f = new File("RSSFeed.xml");
addFeed(new RssReader(f));
cModel.addElement(feedList.get(nextFeed).rssChannel.getTitle());
nextFeed++;
iModel.clear();
}
catch (IOException ee){
System.err.println(ee);
}
}
}
public class RemoveAction extends AbstractAction{
public RemoveAction(String name){
super(name);
}
public void actionPerformed(ActionEvent e){
cModel.removeElement(channelTitleJList.getSelectedValue());
iModel.clear();
}
}
public class ClearAction extends AbstractAction{
public ClearAction(String name){
super(name);
}
public void actionPerformed(ActionEvent e){
iModel.clear();
}
}
private class ChannelListListener implements ListSelectionListener{
public void valueChanged (ListSelectionEvent e) {
/* boolean adjust = e.getValueIsAdjusting();
if (!adjust) {
List<String> titles = new ArrayList<String>();
int i = channelTitleJList.getSelectedIndex(); //the index of the selected item in the left list
for(RssItem feed: feedList.get(i).rssChannel.items) {
titles.add(feed.getTitle()); //build a list of titles, to be shown in the right list
}
itemTitleJList.setListData(titles.toArray()); //change the content of the right list
}*/
}
}
/*
private class ItemListListener implements ListSelectionListener {
public void valueChanged (ListSelectionEvent e) {
boolean adjust = e.getValueIsAdjusting();
if (!adjust) {
int i = e.getLastIndex();
try{
JFrame w = new JFrame();
w.setTitle("Html Display");
w.setSize(1000, 600);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setVisible(true);
JEditorPane htmlPane = new JEditorPane();
htmlPane.setPage(feedList.get(i).rssChannel.items.get(i).getLink());
w.add(new JScrollPane(htmlPane));
}
catch(Exception ee){
ee.printStackTrace();
}
}
}
}
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
GuiDriver frame = new GuiDriver();
frame.setTitle("RSS Reader");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(frame.createJSplitPane(), BorderLayout.NORTH);
frame.add(frame.createToolBar(), BorderLayout.SOUTH);
frame.setVisible(true);
frame.setSize(800,400);
}
});
}
}
어떤 줄에서 오류가 발생합니까? –
그리고 println 디버그 명령문은 어디에 있는지, 예를 들어 ArrayLists에 액세스하는 데 사용 된 색인의 값을 확인합니다. –