如何摆脱JList的预选

时间:2011-06-02 20:26:39

标签: java arraylist selection listener jlist

问题在于,当文件运行时,JList已经自己做出选择,因此当用户进行选择时,列表不再处于正确的位置。最后,我想让它成为一个递归方法,并让用户选择一个单词,然后将其从列表中删除,直到没有剩下的单词...有谁知道如何设置自动选择是否为空?

import java.util.ArrayList;
import java.io.*;

public class Arg{

   public static void main(String[] args) 
   {
       callArray();
   }

   public static void callArray() {

      final JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      final ArrayList alist = new ArrayList();

      alist.add("one");
      alist.add("two");
      alist.add("three");
      alist.add("four");
      alist.add("five");
      alist.add("six");
      alist.add("seven");
      alist.add("eight");

      final JList blist = new JList(alist.toArray());
      blist.clearSelection();

      JScrollPane scroll = new JScrollPane(blist);
      panel.add(blist);
      frame.add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(100, 100);
      frame.setVisible(true);

      final PVariable varPass = new PVariable();

      System.out.println(alist);

      blist.addListSelectionListener(new ListSelectionListener() {
         public void valueChanged(ListSelectionEvent e) {
            varPass.selected = blist.getSelectedIndex();
            System.out.println("The value selected is " + alist.get(varPass.selected) + " " + varPass.selected);
            frame.setVisible(false);
         }
      });

      alist.remove(varPass.selected);

      System.out.println(alist);
   }
}

class PVariable {
    static int selected;

    public PVariable() { }
}

1 个答案:

答案 0 :(得分:0)

  

有谁知道如何将自动选择设置为null?

setSelectedIndex( -1 );