如何摆脱JList的预选

发布于 2024-11-11 15:37:36 字数 1553 浏览 3 评论 0原文

问题是,当文件运行时,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() { }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2024-11-18 15:37:36

有人知道如何将正在进行的任何自动选择设置为空吗?

setSelectedIndex( -1 );

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

setSelectedIndex( -1 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文