我想知道是否可以通过选择带有JList的项来使程序执行一些代码。每当选择一个新项目时,此代码应运行。
以前,我添加了一个侦听器。这是我做的一个最小的例子。
public class Driver {
public static void main(String[] args) {
JFrame frame = new ListFrame();
frame.setVisible(true);
frame.setSize(200,100);
}
}
public class ListFrame extends JFrame {
private JList<String> list;
private JScrollPane scrollPane;
private String[] data = {"A","B","C"};
private JButton addButton = new JButton("Add");
public ListFrame() {
setLayout(new BorderLayout());
list = new JList<String>(data);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane = new JScrollPane(list);
add(scrollPane, BorderLayout.CENTER);
add(addButton, BorderLayout.SOUTH);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String newEntry = JOptionPane.showInputDialog("Add new entry.");
String[] tempData = new String[data.length + 1];
for(int i = 0; i < data.length; i++)
tempData[i] = data[i];
tempData[data.length] = newEntry;
data = tempData;
list = new JList<String>(data);
scrollPane.setViewportView(list);
}
});
list.addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
System.out.println("Hi");
}
});
}
}
但是,当我单击Jlist上的项目时,没有打印任何内容。
最佳答案
您的示例使用getSelectionModel()
获取列表的ListSelectionModel
,并将您的侦听器直接添加到选择模型。这会绕过ListSelectionHandler
内部使用的JList
,而永远不会有机会使用fireSelectionValueChanged()
。相反,让JList
添加您的侦听器:
list.addListSelectionListener(new ListSelectionListener() {...}
当我单击
JList
上的项目时,没有打印任何内容。当您单击一个项目时,您的新示例将打印“ Hi”,但出现一些问题:
确保在event dispatch thread上运行。
检查
ListSelectionEvent
以了解发生的详细信息。要将元素添加到列表中,请勿创建新的
JList
;而是更新列表的ListModel
。有关更多信息,请参见How to Write a List Selection Listener。这是我测试的示例。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Driver {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new ListFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
});
}
private static class ListFrame extends JFrame {
private final String[] data = {"A", "B", "C"};
private final DefaultListModel model = new DefaultListModel();
private final JList<String> list = new JList<>(model);
private final JButton addButton = new JButton("Add");
public ListFrame() {
for (String s : data) {
model.addElement(s);
}
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(new JScrollPane(list), BorderLayout.CENTER);
add(addButton, BorderLayout.SOUTH);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String newEntry = JOptionPane.showInputDialog("Add new entry.");
model.addElement(newEntry);
}
});
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
System.out.println(e.getFirstIndex() + " " + e.getLastIndex());
}
}
});
}
}
}
关于java - JList项目选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32940839/