Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JList DefaultListModel ListSelectionListener_Java_Swing_Jlist_Defaultlistmodel - Fatal编程技术网

Java JList DefaultListModel ListSelectionListener

Java JList DefaultListModel ListSelectionListener,java,swing,jlist,defaultlistmodel,Java,Swing,Jlist,Defaultlistmodel,我在一个JSplitPane中有2个DefaultListModels。JSplitPane的左侧 他有一本书的书名。选择RssChannel标题时,RssItem标题 应该出现在JSplitPane的右侧 选择RssChannel标题的初始时间将显示正确的RssItem标题。 但是当我在RssChannel标题之间来回移动时,正确的RssItem标题 不要开枪 如何修复我的侦听器,使正确的RssItem标题始终从其各自的RssChannel中触发 谢谢你的建议 import javax.swi

我在一个JSplitPane中有2个DefaultListModels。JSplitPane的左侧 他有一本书的书名。选择RssChannel标题时,RssItem标题 应该出现在JSplitPane的右侧

选择RssChannel标题的初始时间将显示正确的RssItem标题。 但是当我在RssChannel标题之间来回移动时,正确的RssItem标题 不要开枪

如何修复我的侦听器,使正确的RssItem标题始终从其各自的RssChannel中触发

谢谢你的建议

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 channelTitleList, itemTitleList;
    DefaultListModel cModel, iModel;
    List<RssReader> feedList = new ArrayList<RssReader>();
    int nextFeed=0;

    public GuiDriver(){
    }

    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 JSpli tPane(JSplitPane.HORIZONTAL_SPLIT,createChannelJScrollPane(), createItemJScrollPane());
       hSplitPane.setDividerLocation(500);
       return hSplitPane;
   }


   public JScrollPane createChannelJScrollPane(){            
      cModel = new DefaultListModel(); 
      channelTitleList = new JList(cModel);
      JScrollPane channelJScrollPane = new JScrollPane(channelTitleList);
      channelTitleList.setVisibleRowCount(20);
      channelTitleList.getSelectionModel.addListSelectionListener(new ChannelListListener());      

      return channelJScrollPane;     
   }


   public JScrollPane createItemJScrollPane(){
      iModel = new DefaultListModel();
      itemTitleList = new JList(iModel);
      JScrollPane itemJScrollPane = new JScrollPane(itemTitleList);

      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(channelTitleList.getSelectedValue());
         feedList.remove(e);
         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) {
            int i = e.getLastIndex();
            for (int j=0; j<feedList.get(i).rssChannel.getItems().size(); j++)
               iModel.addElement(feedList.get(i).rssChannel.items.get(j).getTitle());
         }
      }
   }


   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);
         }   
      });  
   }
}
import javax.swing.*;
导入javax.swing.event.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.List;
导入java.util.ArrayList;
导入java.io.*;
公共类GuiDriver扩展JFrame{
JList channelTitleList,itemTitleList;
DefaultListModel cModel,iModel;
List feedList=新的ArrayList();
int nextFeed=0;
公共驱动程序(){
}
公共无效添加源(RssReader源){
feedList.add(feed);
}
公共JToolBar createToolBar(){
JToolBar=新的JToolBar();
Action newToolBarButton=新添加操作(“新”);
Action deleteToolBarButton=新建删除操作(“删除”);
动作clearToolBarButton=新的ClearAction(“清除”);
添加(新工具栏按钮);
添加(删除工具栏按钮);
添加(ClearToolBar按钮);
可设置浮动条(假);
返回杆;
}
公共JSplitPane createJSplitPane(){
JSplitPane hSplitPane=新的JSpli-tPane(JSplitPane.HORIZONTAL_SPLIT,createChannelJScrollPane(),createItemJScrollPane());
hSplitPane.setdividerllocation(500);
返回窗格;
}
公共JScrollPane createChannelJScrollPane(){
cModel=新的DefaultListModel();
channelTitleList=新的JList(cModel);
JScrollPane channelJScrollPane=新的JScrollPane(channelTitleList);
channelTitleList.setVisibleRowCount(20);
channelTitleList.getSelectionModel.addListSelectionListener(新的ChannelListListener());
返回通道jScrollPane;
}
公共JScrollPane createItemJScrollPane(){
iModel=新的DefaultListModel();
itemTitleList=新的JLList(iModel);
JScrollPane itemJScrollPane=新的JScrollPane(itemTitleList);
返回itemJScrollPane;
}   
公共类AddAction扩展了AbstractAction{
公共AddAction(字符串名称){
超级(姓名);
}
已执行的公共无效操作(操作事件e){
System.out.println(getValue(Action.NAME)+“selected”);
JOptionPane消息=新建JOptionPane();
String firstInput=message.showInputDialog(null,“输入URL”);
试一试{
DumpStockPage.readXml(firstInput);
文件f=新文件(“RSSFeed.xml”);
addFeed(新RssReader(f));
cModel.addElement(feedList.get(nextFeed.rssChannel.getTitle());
nextFeed++;
iModel.clear();
}
捕获(IOEE异常){
系统错误打印项次(ee);
}
}
}
公共类RemoveAction扩展了AbstractAction{
公共RemoveAction(字符串名称){
超级(姓名);
}
已执行的公共无效操作(操作事件e){
cModel.removelement(channelTitleList.getSelectedValue());
删除(e);
iModel.clear();
} 
}
公共类ClearAction扩展了AbstractAction{
公共ClearAction(字符串名称){
超级(姓名);
}
已执行的公共无效操作(操作事件e){
iModel.clear();
} 
}
私有类ChannelListListener实现ListSelectionListener{
public void值已更改(ListSelectionEvent e){
布尔调整=e.getValueIsAdjusting();
如果(!调整){
int i=e.getLastIndex();

对于(int j=0;j我不确定RssReader的实现,但您的
ChannelListListener
会不断向项目列表中添加数据。您需要的是
channelTitleList
中所选项目的标题列表。使用
getSelectedIndex()获取所选索引
JList
。接下来,根据所选提要,构建
项目标题列表
的内容,例如作为字符串列表。以下是一个示例,供
ChannelListListener
开始使用:

public void valueChanged (ListSelectionEvent e) {        
     boolean adjust = e.getValueIsAdjusting();
     if (!adjust) {
        List<String> titles = new ArrayList<String>();
        int i = channelTitleList.getSelectedIndex(); //the index of the selected item in the left list

        //Change RssFeed to the appropriate class for these 'items'
        for(RssFeed feed: feedList.get(i).rssChannel.items) {
            titles.add(feed.getTitle());  //build a list of titles, to be shown in the right list
        }

        itemTitleList.setListData(titles.toArray()); //change the content of the right list
    }
}
public void值已更改(ListSelectionEvent e){
布尔调整=e.getValueIsAdjusting();
如果(!调整){
列表标题=新的ArrayList();
int i=channelTitleList.getSelectedIndex();//左侧列表中选定项的索引
//将RssFeed更改为这些“项”的相应类
for(RssFeed提要:feedList.get(i).rssChannel.items){
titles.add(feed.getTitle());//构建标题列表,显示在右侧列表中
}
itemTitleList.setListData(titles.toArray());//更改右侧列表的内容
}
}

我不确定RssReader的实现,但您的
频道列表监听器一直在向项目列表添加数据。您需要的是
频道标题列表中所选项目的标题列表。
使用
getSelectedIndex()获取所选索引
JList
。接下来,根据所选提要,构建
项目标题列表
的内容,例如作为字符串列表。以下是一个示例,供
ChannelListListener
开始使用:

public void valueChanged (ListSelectionEvent e) {        
     boolean adjust = e.getValueIsAdjusting();
     if (!adjust) {
        List<String> titles = new ArrayList<String>();
        int i = channelTitleList.getSelectedIndex(); //the index of the selected item in the left list

        //Change RssFeed to the appropriate class for these 'items'
        for(RssFeed feed: feedList.get(i).rssChannel.items) {
            titles.add(feed.getTitle());  //build a list of titles, to be shown in the right list
        }

        itemTitleList.setListData(titles.toArray()); //change the content of the right list
    }
}
public void值已更改(ListSelectionEvent e){
布尔调整=e.getValueIsAdjusting();
如果(!调整){
列表标题=新的ArrayList();
int i=channelTitleList.getSelectedIndex();//左侧列表中选定项的索引
//将RssFeed更改为这些“项”的相应类
for(RssFeed提要:feedList.get(i).rssChannel.items){
titles.add(feed.getTitle());//bui