开发者

How to get rid of JList's preselection

The issue is that when the file runs JList already makes its own selection and so when the user makes a selection the list is no longer in the right position. Ultimately, I want to make this a recursive method and have the user select a word and it be removed from the list until there are no words left... Does anyone know how to set the whatever 开发者_如何学Cautoselection is going on to null?

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() { }
}


Does anyone know how to set the whatever autoselection is going on to null?

setSelectedIndex( -1 );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜