如何摆脱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() { }
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)