JList 添加/删除项目
您好,我必须从 JList 中选择一个元素到另一个元素,并将其从第一个元素中删除 我创建的方法仅插入一个元素,覆盖最后一个元素,并且不会从第一个 JList 中删除所选项目 这是代码:
第一个列表
private javax.swing.JList listaRosa;
用此方法填充:
private void visualizzaRosaButtonvisualizzaRosa(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
visualizzaSquadraSelezionata();
String fileSquadra;
fileSquadra = squadraDaVisualizzare.getText();
DefaultListModel listModel = new DefaultListModel();
try {
FileInputStream fstream = new FileInputStream("C:/Users/Franky/Documents/NetBeansProjects/JavaApplication5/src/javaapplication5/Rose/"+fileSquadra+"");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
listModel.addElement(strLine);
System.out.println(strLine);
}
listaRosa.setModel(listModel);
//Close the input stream
in.close();
} catch (Exception e) {
}
第二个列表,我想在其中插入从第一个列表中删除的项目:
private javax.swing.JList listaTitolari
这是不工作的代码:
private void aggiungiTitolareButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DefaultListModel listModel = new DefaultListModel();
String daInserire;
listModel.addElement(listaRosa.getSelectedValue());
listModel.removeElement(listaRosa.getSelectedValue());
listaTitolari.setModel(listModel);
}
谢谢
Hi I have to pick an element from a JList to another, removing it from the first
The method I've created inserts only one element, overwriting the last one and doesn't remove the selected item from the first JList
Here's the code:
First list
private javax.swing.JList listaRosa;
Populated by this method:
private void visualizzaRosaButtonvisualizzaRosa(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
visualizzaSquadraSelezionata();
String fileSquadra;
fileSquadra = squadraDaVisualizzare.getText();
DefaultListModel listModel = new DefaultListModel();
try {
FileInputStream fstream = new FileInputStream("C:/Users/Franky/Documents/NetBeansProjects/JavaApplication5/src/javaapplication5/Rose/"+fileSquadra+"");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
listModel.addElement(strLine);
System.out.println(strLine);
}
listaRosa.setModel(listModel);
//Close the input stream
in.close();
} catch (Exception e) {
}
The second list, where I want to insert items removing from the first:
private javax.swing.JList listaTitolari
Here's the NOT working code:
private void aggiungiTitolareButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DefaultListModel listModel = new DefaultListModel();
String daInserire;
listModel.addElement(listaRosa.getSelectedValue());
listModel.removeElement(listaRosa.getSelectedValue());
listaTitolari.setModel(listModel);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
您可能会添加一个元素并立即删除它,因为添加和删除操作都在同一个 listModel 上。
尝试
The problem is
you may be adding an element and immediatly removing it since both add and remove operations are on the same listModel.
Try
清除 JLIST 的最佳且最简单的方法是:
The best and easiest way to clear a JLIST is: