0
我可以添加一个元素到我的JList
,但是如何删除我选择的元素?如何从我的jlist中删除文本?
这里是我的代码:
DefaultListModel<String> model = new DefaultListModel<>();
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// int n = JOptionPane.showConfirmDialog(Jframe.this,"Clicked?");System.out.println(n);
String name = textfield1.getText();
model.addElement(name);
custList.setModel(model);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
你需要作用于点击的方法来获取对象的ID,然后从JList中删除它的另一种方法。 – duffymo
也许['DefaultListModel#removeElement'](https://docs.oracle.com/javase/8/docs/api/javax/swing/DefaultListModel.html#removeElement-java.lang.Object-)或[DefaultListModel#' removeElementAt'](https://docs.oracle.com/javase/8/docs/api/javax/swing/DefaultListModel.html#removeElementAt-int-)? – bradimus
请举例说明这对我很重要 – kingramx