简体   繁体   English

JList DefaultListModel ListSelectionListener

[英]JList DefaultListModel ListSelectionListener

I have 2 DefaultListModels inside a JSplitPane. 我在JSplitPane中有2个DefaultListModels。 The left side of the JSplitPane has RssChannel titles. JSplitPane的左侧具有RssChannel标题。 When a RssChannel title is selected, the RssItem titles are supposed to show up on the right side of the JSplitPane. 当选择了RssChannel标题时,RssItem标题应该显示在JSplitPane的右侧。

The initial time RssChannel title is selected the correct RssItem titles appear. 选择初始时间RssChannel标题后,将显示正确的RssItem标题。 But when I go back and forth between the RssChannel titles, the correct RssItem titles don't fire. 但是,当我在RssChannel标题之间来回切换时,不会触发正确的RssItem标题。

How can I fix my Listener so that the correct RssItem titles are always fired from their respective RssChannel? 如何修复我的侦听器,以便始终从各自的RssChannel触发正确的RssItem标题?

Thanks for any suggestions 感谢您的任何建议

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);
         }   
      });  
   }
}

I'm not sure about the implementation of RssReader, but your ChannelListListener keeps adding data to your item list. 我不确定RssReader的实现,但是您的ChannelListListener会不断向您的项目列表添加数据。 What you want is a list of titles for the selected item from channelTitleList . 您需要的是channelTitleList所选项目的标题列表。 Get the selected index using getSelectedIndex() of JList . 使用JList getSelectedIndex()获取选定的索引。 Next, build the content of itemTitleList , for instance as a list of strings, based on the selected feed. 接下来,根据所选的提要,构建itemTitleList的内容,例如以字符串列表的形式。 Here's an example for the ChannelListListener to get you started: 这是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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM